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;*/
}
sanastasiadis
  • 1,182
  • 1
  • 15
  • 23

2 Answers2

1

Finally i found the solution, the main problem is that in my Eclipse project i Enable Spring Aspects tooling in the option menu of Spring tools (Right click on project) and that in some manner was compiling my aspects with traditional Aspectj before Spring AOP, so that Explains why no matter which Conditional i was using over the aspect was always applied.

So the solution is do not enable Spring Aspectj Tools. or if is enabled do right click in project AspectJ Tools -> Remove AspectJ Capability.

0

You could use @Conditional annotation or use a PropertySourcesPlaceholderConfigurer in xml bean defintion files.

For example, for xml

test.aspect = org.example.Test1Aspect

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

You don't need maven aspectj plugin for Spring AOP.

Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38
  • i forgot to tell that i'm using only java config, the only xml in my project is my pom.xml, i could do this using java config? – Diego Fernando Murillo Valenci Aug 21 '14 at 00:07
  • i use `@Conditional` and the conditional is called returning false but the aspect is called and the class is extended anyway, maybe is an Spring bug? i configure my aspect with `@Component` annotation. – Diego Fernando Murillo Valenci Aug 21 '14 at 00:30
  • @DiegoFernandoMurilloValenci Are you still weaving with aspectj plugin? – Jose Luis Martin Aug 21 '14 at 10:07
  • No, i remove it and use the `@EnableAspectJAutoProxy` in my configuration class, then i create a `@Bean` of my aspect and add the conditional, the conditional lets disable the aspect bean declaration (i put a logger in the aspect constructor for testing if is called) but when the bean is not initialized the advices in the aspect keeps working, i test if Spring compiles the aspect Autowiring a service and logging it in an advice (null if the condition is false), is like my project compiles anyway the aspects using or not Spring AOP – Diego Fernando Murillo Valenci Aug 21 '14 at 13:50