【问题标题】:AOP (without Spring) not working on Tomcat but EclipseAOP(没有 Spring)不适用于 Tomcat 但 Eclipse
【发布时间】:2015-05-26 01:31:05
【问题描述】:

在没有 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"

【问题讨论】:

  • 这里有一个类似的问题:stackoverflow.com/questions/10032092/…,一个可能的答案是确保 aop.xml 文件位于正确的位置。如果您没有得到与编织相关的任何输出,那一定是代理设置不正确或 aop.xml 放置在错误的位置,而不是方面有任何特别错误。
  • @AndyClement :aop.xml 的位置很好。找到了解决方案。我不得不在 maven 中添加 aspectj-maven-plugin。

标签: java tomcat aop aspectj


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2014-06-22
    • 2015-06-04
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多