【发布时间】: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。