Skip to content

IllegalAccessError for some auto-configured beans when enabling AspectJ load time weaving #31300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pdeneve opened this issue Sep 23, 2023 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on

Comments

@pdeneve
Copy link
Contributor

pdeneve commented Sep 23, 2023

Created this issue as suggested here.

To enable AspectJ load time weaving in a Spring application, one would use the @EnableLoadTimeWeaving annotation as outlined in the Spring reference documention. Due to issue 29609 this is not working and a workaround proposed involves registering AspectJWeavingEnabler and DefaultContextLoadTimeWeaver upfront when instantiating the application context. For a spring boot application, this looks as follows:

SpringApplication.run(new Class[] { Config.class, AspectJWeavingEnabler.class, MyLoadTimeWeaver.class }, args);

With MyLoadTimeWeaver extending DefaultContextLoadTimeWeaver and annotated with @Component("loadTimeWeaver") because the name of the LoadTimeWeaver bean "loadTimeWeaver" is expected by the framework.

When running the application, some auto-configured beans cannot be accessed anymore. E.g. for the spring.boot.ltw module in this repository, the following stacktrace is produced:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: failed to access class org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails from class org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor (org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails is in unnamed module of loader 'app'; org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor is in unnamed module of loader org.springframework.context.support.ContextTypeMatchClassLoader$ContextOverridingClassLoader @159e366)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:550) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1155) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2]
        at be.pdn.training.aspectj.spring.boot.ltw.Main.main(Main.java:9) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: failed to access class org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails from class org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor (org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails is in unnamed module of loader 'app'; org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor is in unnamed module of loader org.springframework.context.support.ContextTypeMatchClassLoader$ContextOverridingClassLoader @159e366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:605) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1417) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:888) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-6.0.11.jar:6.0.11]
        ... 19 common frames omitted
Caused by: java.lang.IllegalAccessError: failed to access class org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails from class org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor (org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails is in unnamed module of loader 'app'; org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor is in unnamed module of loader org.springframework.context.support.ContextTypeMatchClassLoader$ContextOverridingClassLoader @159e366)
        at org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetailsBeanPostProcessor.postProcessBeforeInitialization(JdbcConnectionDetailsBeanPostProcessor.java:54) ~[spring-boot-autoconfigure-3.1.2.jar:3.1.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:419) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-6.0.11.jar:6.0.11]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598) ~[spring-beans-6.0.11.jar:6.0.11]
        ... 29 common frames omitted

To reproduce yourself:

  • Clone this repository
  • Checkout branch spring_boot_issue
  • Execute mvn compile -pl spring.boot.ltw
  • Make sure JAVA_HOME points to a Java 21 JDK
  • Execute mvn dependency:properties exec:exec -pl spring.boot.ltw

It is possible to workaround this issue by defining manually the beans for which an IllegalAccessError occurs. E.g. for the sample project, uncomment the @Bean annotation for the JdbcConnectionDetails bean in Config.java and it will work.

@snicoll
Copy link
Member

snicoll commented Dec 21, 2023

@pdeneve I don't think the instruction to reproduce are correct. Shouldn't we do something before exec:exec?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Dec 21, 2023
@pdeneve
Copy link
Contributor Author

pdeneve commented Dec 23, 2023

@snicoll Thanks for having a look. You're right, you need to run mvn compile before. I've updated the instructions. Please note I've just updated all dependencies, so please pull. The updated instructions apply to the new version.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 23, 2023
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

No branches or pull requests

4 participants