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