I have a Springboot service. I am using Aspectj AOP for access control. I have an aspect class AuthorizationAspect with an autowired field with some access control methods. The autowired field is always null.
Aspect class:
@Aspect
public class AuthorizationAspect {
@Autowired
private UserService userService;
@Around(
"execution(* some.controller.Method(..))"
)
public ResponseEntity<?> checkIfAdmin(ProceedingJoinPoint joinPoint) throws Throwable {
// do something with userService
}
...
}
The field userService is always null.
I've tried adding @Configurable(autowire = Autowire.BY_TYPE)
to the Aspect class and adding <bean id="authorizationAspect" class="com.example.accesscontrol.AuthorizationAspect" factory-method="aspectOf"></bean>
to beans.xml, but nothing seems to work.
How can I resolve this issue?
Thanks for the MCVE. It was key to understanding your situation. I sent you a pull request fixing the problem.
Basically, you need to follow the Spring manual chapter Using AspectJ to Dependency Inject Domain Objects with Spring and use @Configurable
on your native aspect and @EnableSpringConfigured
on your Spring application. Last but not least, you need a factory method for the native aspect to make it known to Spring:
@Bean
public AuthorizationAspect authorizationAspect() {
return Aspects.aspectOf(AuthorizationAspect.class);
}
The important commit from my PR is acb5da67.