Java 春季AOP@declarePairs有条件

Java 春季AOP@declarePairs有条件,java,spring,aspectj,spring-aop,aspectj-maven-plugin,Java,Spring,Aspectj,Spring Aop,Aspectj Maven Plugin,有一种方法可以有条件地创建方面介绍吗?我想有条件地使用Spring AOP扩展一个类: @Aspect public class Test1Aspect { @DeclareParents(value="com.test.testClass",defaultImpl=Test1Impl.class) public ITest iTest; } @Aspect public class Test2Aspect { @DeclareParents(value="com.te

有一种方法可以有条件地创建方面介绍吗?我想有条件地使用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插件,但它不排除我的方面:

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>
testspect

//@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;*/
}

您可以在XMLBean定义文件中使用
@Conditional
注释或使用
属性资源占位符配置器

例如,对于xml

test.aspect = org.example.Test1Aspect

<context:property-placeholder location="configuration.properties" /> 
<bean id="testAspect" class="${test.aspect}" />
test.aspect=org.example.Test1Aspect

Spring AOP不需要maven aspectj插件。

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


因此,解决方案是不要启用SpringAspectJ工具。或者,如果启用了,请在project AspectJ Tools->Remove AspectJ Capability中单击鼠标右键。

我忘了告诉您我只使用java配置,我的项目中唯一的xml是我的pom.xml,我可以使用java配置来实现这一点?我使用
@Conditional
,条件被称为returning false,但方面被调用,类被扩展,可能是Spring bug?我用
@Component
注释配置我的方面。@Diegornandomurillovalenci你还在用aspectj插件编织吗?不,我删除它并在我的配置类中使用
@enableSpectjautoproxy
,然后我创建我的方面的
@Bean
,并添加条件,条件允许禁用aspect bean声明(我在aspect构造函数中放置了一个记录器,用于测试是否调用了aspect),但当bean未初始化时,aspect中的通知将继续工作,我测试Spring是否编译aspect自动连接服务并将其记录在通知中(如果条件为false,则为null),就像我的项目不管用不用SpringAOP编译方面一样
test.aspect = org.example.Test1Aspect

<context:property-placeholder location="configuration.properties" /> 
<bean id="testAspect" class="${test.aspect}" />