AOP(没有 Spring)不能在 Tomcat 上工作,但在 Eclipse 上工作

AOP (without Spring) not working on Tomcat but Eclipse

在没有 Spring 的情况下使用 AspectJ 实现了 AOP。它在 Eclipse(Tomcat 服务器)中 运行 时工作得很好,但在 Tomcat 中直接 运行 时就不行了。在 pom 中添加了必需的依赖项但没有用。无法找出问题。

看点class:

@Aspect
public class FeatureAOP {
  private static final Logger LOG = LoggerFactory.getLogger(FeatureAOP.class);

  @Pointcut("execution(* x.y.z.rest.ModifiersFacadeWrapper.*(..)) && !execution(* x.y.z.rest.ModifiersFacadeWrapper.getUriInfo(..))")
  protected void pointCut() {
  }

  @Before("x.y.z.rest.aop.FeatureAOP.pointCut()  && this(mf) ")
  public void parseParams(JoinPoint jp, ModifiersFacadeWrapper mf) {
    LOG.info("Entered JoinPoint: {}", jp.getSignature());
    String feature = mf.getUriInfo().getPathParameters().get("feature").get(0);
    Feature featureEnum = Feature.get(feature);
    mf.setFeature(featureEnum);
    LOG.info("Feature set: {}", mf.getFeature());
  }
}

aop.xml:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <weaver options="-verbose -showWeaveInfo -Xset:weaveJavaxPackages=true -debug">
        <include within="x.y.z"/>
    </weaver>
 <aspects>
  <aspect id="featureAspect" class="x.y.z.rest.aop.FeatureAOP" ></aspect>
 </aspects>
</aspectj>
  

阅读一些 post 以将 Tomcat 中的 javaagent 设置为 aspectjweaver 库。这也没有帮助。

export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/home/sumit/Downloads/apache-tomcat-8.0.17/webapps/dpi-manager/WEB-INF/lib/aspectjweaver-1.8.5.jar"

使用 maven 找到解决方案。需要在 pom.xml.

中添加 aspectj-maven-plugin
< plugin >
  < groupId > org.codehaus.mojo < /groupId>
    <artifactId>aspectj-maven-plugin</artifactId >
    < version > 1.4 < /version>
    <configuration>
        <source>1.7</source >
        <target > 1.7 < /target>
    </configuration >
  <executions >
    <execution >
      <goals>
      <goal>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>

示例:https://github.com/mscharhag/blog-examples/blob/master/exception-translation/pom.xml