So I want to use Spring Aspect specific to my classes, when the profile is active, but I can not find a solution, I try the method proposed in http://city81.blogspot.com/2012/05/using-spring-profile-with .html , but it is quite old and does not work in my case, I have a Spring Started project for testing, and I am doing the following based on the link:
application setup:
@Configuration @ComponentScan(basePackages= { "demo", "demo.aspect" }) @EnableAutoConfiguration(exclude=AopAutoConfiguration.class) @EnableAspectJAutoProxy(proxyTargetClass=true) public class Application { @Bean @Profile("asdasd")
My aspect:
//TESTING IN ALL THIS WAYS BUT NOTHING //@Component //@Profile("asdasd") @Configurable //@Configuration @Aspect public class TestAspect{ public static final Logger LOGGER = LogManager.getLogger(testControllerEX.class); @Autowired private testService testService; public TestAspect() { LOGGER.info("TEST ASPECT INITIALIZED"); } /*@Before("execution(* demo.testControllerEX.test(*)) && args(param)") public void beforeSampleMethod(Object param) { LOGGER.info("ASPECT" + param.getClass()); }*/ @Before("execution(demo.testControllerEX.new())") public void constructor(JoinPoint point) { LOGGER.info("ASPECT CONSTRUCTOR" + point.getThis().getClass().getAnnotation(Controller.class)); LOGGER.info("SERVICE" + testService); } @Around("execution(* demo.testControllerEX.testPrevent(*)) && args(param)") public String prevent(ProceedingJoinPoint point, String param) throws Throwable{ //LOGGER.info("ASPECT AROUND" + param); LOGGER.info("ASPECT AROUND " + testService); String result = (String)point.proceed(); return result; } /*@DeclareParents(value="(demo.testControllerEX)",defaultImpl=testControllersssImpl.class) private ITestControllerEX itestControllerEX;*/ }
Finally, I try to apply my aspect to the controller constructor, it works, but I need to apply when the profile is active, and the other error that I found is that my testService is initialized after the pointcut constructor, so it is null, but in the testPrevent method works, obviously, the service is initialized earlier, I can take another form that completed what I want
EDIT
I got that my testService loaded befome my pointcut constructor, but remains null:
@Configuration @ComponentScan(basePackages= { "demo", "demo.aspect" }) @EnableAutoConfiguration(exclude=AopAutoConfiguration.class) @EnableAspectJAutoProxy(proxyTargetClass=true) public class Application { @Autowired private testService testService; ...
source share