1

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • You haven't provided any information about how you configured the external Tomcat instance where you say it worked. I suspect the difference in behaviour lies in a difference in the configuration. – Andy Wilkinson Mar 20 '17 at 20:28
  • Well, don't have much interesting to say here. I took vanilla tomcat > 8.0.28, extended its CATALINA_OPTS in startup.sh by adding -javaagent:$CATALINA_BASE/aspectjweaver-1.8.10.jar \ -javaagent:$CATALINA_BASE/spring-instrument-4.3.4.RELEASE.jar I tried it both in Intellij IDEA and on CentOS 6. JDK 1.8u > 102 was used for experiments – Dmitry Kotlov Mar 21 '17 at 05:50
  • @AndyWilkinson I created an example project for you https://github.com/karuniko/springboot-aspectj-example Could you please take a look? The issue is reproduced there – Dmitry Kotlov Mar 27 '17 at 15:53
  • One note: it's only reproducible when devtools jar is defined as a dependency – Dmitry Kotlov Mar 27 '17 at 17:55

0 Answers0