0

我使用 Eclipse 开普勒,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 clean compile输出:

[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] ------------------------------------------------------------------------
4

2 回答 2

3

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

<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>
于 2015-07-12T14:14:20.787 回答
0

您是否创建了一个 aop.xml 并放置在 Meta-inf 文件夹下,并且在该 aop.xml 中您是否放置了需要编织的类的包详细信息?

于 2015-02-09T16:45:04.370 回答