gpt4 book ai didi

java - Spring AOP 有条件地@DeclareParents

转载 作者:行者123 更新时间:2023-12-01 12:37:51 26 4
gpt4 key购买 nike

有一种方法可以有条件地创建 Aspect 引入吗?我想要的是有条件地使用 Spring AOP 扩展一个类:

@Aspect
public class Test1Aspect {
@DeclareParents(value="com.test.testClass",defaultImpl=Test1Impl.class)
public ITest iTest;
}

@Aspect
public class Test2Aspect {
@DeclareParents(value="com.test.testClass",defaultImpl=Test2Impl.class)
public ITest iTest;
}

所以 testClass 扩展 Test1Impl 或 Test2Impl 取决于我设置该选项的属性文件,这可能吗?我如何排除被调用的方面,我尝试使用aspectj-maven-plugin,但它不排除我的方面:

pom.xml

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<sources>
<source>
<basedir>src/main/java</basedir>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

编辑

我删除了aspectj-maven-plugin并仅使用Spring AOP,以下是配置和测试方面:

应用程序.java

@Configuration
@ComponentScan(basePackages= {
"demo"
//"demo.aspect"
})
@EnableAutoConfiguration(exclude=AopAutoConfiguration.class)
//@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy
public class Application {

public static final Logger LOGGER = LogManager.getLogger(Application.class);

@Bean
public testService testService() {
return new testService();
}

@Bean
@Conditional(TestCondition.class) //CLASS THAT ONLY RETURNS TRUE OR FALSE
public TestAspect testAspect() {
LOGGER.info("TEST ASPECT BEAN");
TestAspect aspect = Aspects.aspectOf(TestAspect.class);
return aspect;
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

测试方面

//@Component
//@Profile("asdasd")
//@Configurable
//@Configuration
@Aspect
public class TestAspect{
public static final Logger LOGGER = LogManager.getLogger(TestAspect.class);

@Autowired
private testService testService;

public TestAspect() {
LOGGER.info("TEST ASPECT INITIALIZED");
}

@Around("execution(* demo.testControllerEX.test(*))")
public String prevent(ProceedingJoinPoint point) throws Throwable{
LOGGER.info("ASPECT AROUND " + testService); // ALWAYS CALLED NO MATTER IF THE CONDITION IS FALSE, THE ONLY DIFFERENCE IS THAT testService IS NULL WHEN THE CONDITION IS FALSE.
String result = (String)point.proceed();
return result;
}

/*@DeclareParents(value="(demo.testControllerEX)",defaultImpl=TestControllersImpl.class)
private ITestControllerEX itestControllerEX;*/
}

最佳答案

最后我找到了解决方案,主要问题是在我的 Eclipse 项目中,我在 Spring 工具的选项菜单中启用 Spring Aspects 工具(右键单击项目)并且以某种方式进行编译我在 Spring AOP 之前使用传统 Aspectj 的方面,因此解释了为什么无论我在方面使用哪个条件,总是应用该方面。

所以解决方案是不要启用Spring Aspectj Tools。或者如果已启用,请右键单击项目 AspectJ 工具 -> 删除 AspectJ 功能。

关于java - Spring AOP 有条件地@DeclareParents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410144/

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