java - 编译时方面编织不起作用

标签 java aop aspectj aspect aspectj-maven-plugin

我使用 Eclipse Kepler、Java 1.7。 我的 pom.xml 的一部分如下。 正如我在 Maven 编译期间看到的那样,根本没有关于编织的日志。我也没有任何错误。 方面也不起作用。 我究竟做错了什么? 正如我在一些示例中看到的,这个 pom 应该可以工作。 我将 AspectJ 工具安装到 Eclipse 中。

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <source>${compiler.version}</source>
                <target>${compiler.version}</target>
                <Xlint>ignore</Xlint>
                <complianceLevel>${compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            </plugin>

编辑: 这是我的方面代码:

package util;

import java.util.logging.Logger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;

@Aspect
public class MyAspect {
    private static final Logger logger = Logger.getLogger(MyAspect.class.getName());

    @Around("execution(public * filter.RolesFilter.doFilter(..))")
    public Object loggingMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        Object retVal = joinPoint.proceed();

        stopWatch.stop();

        StringBuffer logMessage = new StringBuffer();
        logMessage.append(joinPoint.getTarget().getClass().getName());
        logMessage.append(".");
        logMessage.append(joinPoint.getSignature().getName());
        logMessage.append("(");
        // append args
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            logMessage.append(args[i]).append(",");
        }
        if (args.length > 0) {
            logMessage.deleteCharAt(logMessage.length() - 1);
        }
        logMessage.append(")");
        logMessage.append(" execution time: ");
        logMessage.append(stopWatch.getTotalTimeMillis());
        logMessage.append(" ms");
        System.out.println(logMessage.toString());
        logger.info(logMessage.toString());
        return retVal;
    }

}

我想用我的方面编织的方法:

package filter;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RolesFilter implements Filter {
    Logger logger = Logger.getLogger(RolesFilter.class.getName());

    public RolesFilter() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException {
        logger.info("This is logger message");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        String reqURI = req.getRequestURI();
        String userName = (String) session.getAttribute("username");
        Character role = (Character) session.getAttribute("role");
        try {
            if (role != null && role.charValue() == 'A') {
                if (reqURI.indexOf("/admin/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/view/roles/admin/home.xhtml");
                }
            } else if (role != null && role.charValue() == 'B') {
                if (reqURI.indexOf("/biller/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/biller.xhtml");
                }
            } else if (role != null && role.charValue() == 'C') {
                if (reqURI.indexOf("/customer/") >= 0) {
                    chain.doFilter(request, response);
                } else {
                    res.sendRedirect(req.getContextPath()
                            + "/customer.xhtml");
                }
            } else {
                res.sendRedirect(req.getContextPath()
                        + "/login.xhtml");
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

此外,这里是 maven cleancompile 输出:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for myproject:war:0.0.1-SNAPSHOT
[WARNING] The expression ${build.sourceDirectory} is deprecated. Please use ${project.build.sourceDirectory} instead.
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject ---
[INFO] Deleting /Users/user1/git/myproject/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.1:compile (default-compile) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 41 source files to /Users/user1/git/myproject/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.007 s
[INFO] Finished at: 2015-02-16T10:00:23-08:00
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------

最佳答案

您的 Maven 日志根本没有显示 aspectj-maven-plugin 正在执行的迹象,因此没有编织。我想您已将插件配置放入 <pluginManagement>部分,但尚未引用 <plugins> 中配置的插件部分。这可以解释缺少的插件输出。 IE。你需要这样做:

<pluginManagement>
    <plugins>
        <!-- Configure plugin here -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                (...)
            </configuration>
            (...)
    </plugins>
</pluginManagement>

(...)

<plugins>
    <!-- Refer to the pre-configured plugin here in order to actually use it -->
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
    </plugin>
</plugins>

关于java - 编译时方面编织不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28414713/

相关文章:

java - 代号一 showNativePicker() 方法问题

c# - 如何对 PostSharp 方面进行单元测试?

java - 与接口(interface)上的注释匹配的 Spring AOP 切入点

java - 如何将 AspectJ 加载时编织与 Spring AOP 结合使用?

java - 检查是否加载了 aspectjweaver(或任何 javaagent)

java - 无法在 Java 正则表达式中使用 '-' 字符,它在给定文本中找不到模式

java - 将 LocalDate 解析为 hashMap Token

java - JEdi​​torPane 中的换行符

java - Spring Controller 的 Aop 注释不起作用

spring - 使用 Spring 和 AspectJ 可配置与组件