0

There is a method for create an Aspect introduction conditionally? what i want is to extend a class using Spring AOP conditionally:

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

so testClass extends Test1Impl or Test2Impl depending of a properties file where i set that option, its possible? how i can exclude Aspects for being called, i try to use aspectj-maven-plugin but it don't exclude my Aspects:

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>

EDIT

I remove the aspectj-maven-plugin and using only Spring AOP, following is the configuration and the test aspect:

Aplication.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);
    }
}

TestAspect

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

2 回答 2

1

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

所以解决方案是不启用 Spring Aspectj Tools。或者如果启用,请右键单击项目 AspectJ Tools -> Remove AspectJ Capability。

于 2014-08-21T14:55:49.627 回答
0

您可以使用@Conditional注释或PropertySourcesPlaceholderConfigurer在 xml bean 定义文件中使用。

例如,对于 xml

test.aspect = org.example.Test1Aspect

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

Spring AOP 不需要 maven aspectj 插件。

于 2014-08-20T21:26:03.833 回答