AOP(没有Spring)不是在Tomcat上工作,而是在Eclipse上



使用没有Spring的AspectJ实现AOP。它在Eclipse (Tomcat服务器)中运行时非常好,但直接在Tomcat中运行时就不行。在pom中添加了所需的依赖项,但没有用。无法解决问题。

方面:

@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>
  

阅读几篇文章,将Tomcat中的javaagent设置为aspectjweaver lib。那也没用。

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

最新更新