java - Spring AOP 有条件地@DeclareParents

标签 java spring aspectj spring-aop aspectj-maven-plugin

有一种方法可以有条件地创建 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/

相关文章:

Java同步和性能方面

java - spectJ中的cflow可以检测跨线程执行吗?

java - 使用ArrayList实现循环队列

Java类对象加密/解密和对象数据库

Java - Binary Search Node 如何检查对象?

java - 有java注解列表吗?

java - 带有 Spring 的嵌入式 MongoDB 无法正常工作

java - XML - 在节点上使用文档方法

spring - 应用启动运行时如何重新加载WebSecurityConfigurerAdapter的Configure方法

java - 添加aspectJ语言来定义连接点