Using Spring @Profile in @Aspect

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") //testing profile to bean level public TestAspect testAspect() { //this allow @autowired in my aspect TestAspect aspect = Aspects.aspectOf(TestAspect.class); return aspect; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

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; ... 
+5
source share
1 answer

Unfortunately, you will not be able to achieve what you want to do. With pure Spring AOP, there is no way to apply advice to calling a constructor, so your only way is to use LTW (Load Time Weaving) with AspectJ.

Link to Spring AOP Link (Spring AOP Features and Goals)

Spring Currently, AOP only supports method execution connection points (advice on the implementation of methods on Spring beans).

You may have already read about LTW to intercept your constructor call, but your Aspect will not be a Spring Bean, so you won’t be able to enter your TestService . Similarly, you are trying to install Profile on a non-Spring Bean (since your advice will not be managed by Spring), so you won’t be able to activate it depending on which Spring profiles you are active.

By the way, using profiles in conjunction with Spring AOP is fully supported as long as you stay with the managed aspects of Spring.

It would be interesting to know more about what you want to do, maybe what you are trying to do is doable without using a constructor call interceptor!

+1
source

Source: https://habr.com/ru/post/1200332/


All Articles