gpt4 book ai didi

java - AOP(没有 Spring)不适用于 Tomcat 但 Eclipse

转载 作者:行者123 更新时间:2023-11-28 22:54:49 37 4
gpt4 key购买 nike

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

关于java - AOP(没有 Spring)不适用于 Tomcat 但 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30436476/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com