I'm using pretty recent springboot v1.5.2 with aspectj's (v1.8.10) load time weaving. I was able to inject a spring bean into my aspectj aspect while running my application on an external tomcat and now I wonder why just the same failed for the embedded tomcat?
springboot: 1.5.2
Aspectj: 1.8.10
Spring instrumentation: 4.3.7.RELEASE
maven command: mvn spring-boot:run -Drun.jvmArguments=" -javaagent:aspectjweaver-1.8.10.jar -javaagent:spring-instrument-4.3.7.RELEASE.jar -Dserver.port=8080 -Dserver.contextPath=/custom-application"
aspect config:
@Configuration
@EnableLoadTimeWeaving
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AspectConfig {
@Bean
public RuntimeContainerDelegateAspect runtimeContainerDelegateAspect() {
return Aspects.aspectOf(RuntimeContainerDelegateAspect.class);
}}
aspect:
@Aspect
public class RuntimeContainerDelegateAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeContainerDelegateAspect.class);
@Autowired
private RestApplicationDeployer deployer;
@Pointcut("execution(* org.someapp.container.impl.RuntimeContainerDelegateImpl.deployApplication(..))")
public void deployApplication() {
}
@Pointcut("execution(* org.someapp.container.impl.RuntimeContainerDelegateImpl.undeployProcessApplication(..))")
public void undeployApplication() {
}
@Around("deployApplication()")
public void aroundDeploy(ProceedingJoinPoint pjp) throws Throwable {
if (LOGGER.isInfoEnabled())
LOGGER.info("RuntimeDelegateCalled: " + pjp.toString());
AbstractProcessApplication app = (AbstractProcessApplication) pjp.getArgs()[0];
LOGGER.debug("deployer={}", deployer);
deployer.deploy(app);
}
@Around("undeployApplication()")
public void aroundUndeploy(ProceedingJoinPoint pjp) throws Throwable {
if (LOGGER.isInfoEnabled())
LOGGER.info("RuntimeDelegateCalled: " + pjp.toString() + "\n" +
"Default implementation does nothing.");
}
}
deployer:
@Component
public class RestApplicationDeployer {
//it's just an ordinary spring bean
}
Actual result: deployer = null, since aspectj creates new instance of the RuntimeContainerDelegateAspect for processing @Around advice
Expected result: deployer = spring bean RestApplicationDeployer
P.S: This works perfectly on an external tomcat instance