Issue with Spring 5.3.3 -> Main Class
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);
}
ConfigTest.class (in the same package of Main Class)
@Configuration
@PropertySource("application.properties")
public class ConfigTest {
@Value(value = "test.value")
private String test;
/**
* @return the test
*/
public String getTest() {
return test;
}
application.properties file
test.value = this is a test!
module-info.java
module ceschi.owm {
requires spring.context;
requires spring.beans;
exports it.ceschi.owm;
}
when I try to run it, the following error happen:
Exception in thread "main" org.springframework.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->module ceschi.owm does not open it.ceschi.owm to unnamed module @25bfcafd
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:514)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572)
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419)
at spring.context@5.3.3/org.springframework.context.annotation.ConfigurationClassEnhancer.createClass(ConfigurationClassEnhancer.java:137)
at spring.context@5.3.3/org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:109)
at spring.context@5.3.3/org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:443)
at spring.context@5.3.3/org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:273)
at spring.context@5.3.3/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:299)
at spring.context@5.3.3/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:134)
at spring.context@5.3.3/org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:751)
at spring.context@5.3.3/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:569)
at spring.context@5.3.3/org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93)
at ceschi.owm/it.ceschi.owm.OWMMain.main(OWMMain.java:29)
Caused by: java.lang.IllegalAccessException: module ceschi.owm does not open it.ceschi.owm to unnamed module @25bfcafd
at java.base/java.lang.invoke.MethodHandles.privateLookupIn(MethodHandles.java:255)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:508)
... 22 more
As for what the unnamed module can be, i don't know. I tried to open in module-info my module, but nothing changed.
I even opened the module to all, to java.lang, java.base or to cglib.
Running on Windows 10 with JDK 15.0.2 or OpenJDK 15.0.1, same issue.
I also tried removing the @Value in the config Class, with no change.
The only thing that work is changing
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigTest.class);
to
ApplicationContext context = new AnnotationConfigApplicationContext();
or putting Maven dependency to Classpath and not to Modulepath.
Comment From: jhoeller
From a quick glance, it should help to declare your config class as @Configuration(proxyBeanMethods=false)
, or simply omitting the @Configuration
annotation completely. That said, an opens
declaration for ALL-UNNAMED
should also work.
A standard configuration class leads to the create of a CGLIB subclass at runtime (similar to a target-class AOP proxy but for a different purpose), and that particular part leads to the illegal access exception. We need to investigate why the unnamed module is involved there to begin with; that subclass should rather live in the same module than your config class.
Comment From: DarkCeschi
Using anyone of the 3 way (change @Configuration, omitting @Configuration,
add opens ALL_UNNAMED), same result.
A slight different error:
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'configTest': Injection of autowired dependencies failed;
nested exception is java.lang.reflect.InaccessibleObjectException: Unable
to make field private java.lang.String it.ceschi.owm.ConfigTest.test
accessible: module ceschi.owm does not "opens it.ceschi.owm" to unnamed
module @2631f68c
at spring.beans@5.3.3
/org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1415)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:608)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at spring.beans@5.3.3
/org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at spring.context@5.3.3
/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)
at spring.context@5.3.3
/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)
at spring.context@5.3.3
/org.springframework.context.annotation.AnnotationConfigApplicationContext.
Il giorno lun 25 gen 2021 alle ore 11:46 Juergen Hoeller notifications@github.com ha scritto:
From a quick glance, it should help to declare your config class as @Configuration(proxyBeanMethods=false), or simply omitting the @Configuration annotation completely. That said, an opens declaration for ALL-UNNAMED should also work.
A standard configuration class leads to the create of a CGLIB subclass at runtime (similar to a target-class AOP proxy but for a different purpose), and that particular part leads to the illegal access exception. We need to investigate why the unnamed module is involved there to begin with; that subclass should rather live in the same module than your config class.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-framework/issues/26440#issuecomment-766726622, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASRCK4IUWNRP4UNE3RGBS4LS3VDXPANCNFSM4WRQVS4Q .
Comment From: jhoeller
You did get one step further at least :-) This is a different cause: Your private @Value
field which will require an opens
declaration in any case since exports
only covers the public surface of your classes. Alternatively, you could turn that field into a public setter method annotated with the same @Value
annotation.
It looks like your opens
declaration does not work as intended. Please check the ALL-UNNAMED
spelling (IIRC it only works with the regular hyphen, not with an underscore). Also, it might help to explicitly declare requires spring.core
since that's where Spring's reflection infrastructure lives, although I would not expect that to be necessary.
Comment From: DarkCeschi
Yes, that was my fault (mind already turned off... it happens). I put the @Value on the variable, not on the setter method. Now it work. Many Thanks
Il giorno lun 25 gen 2021 alle ore 12:49 Juergen Hoeller notifications@github.com ha scritto:
You did get one step further at least :-) This is a different cause: Your private @Value field which will require an opens declaration in any case since exports only covers the public surface of your classes. Alternatively, you could turn that field into a public setter method annotated with the same @Value annotation.
It looks like your opens declaration does not work as intended. Please check the ALL-UNNAMED spelling (IIRC it only works with the regular hyphen, not with an underscore). Also, it might help to explicitly declare requires spring.core since that's where Spring's reflection infrastructure lives, although I would not expect that to be necessary.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/spring-projects/spring-framework/issues/26440#issuecomment-766761664, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASRCK4MCKXQ4AMQK34BXH2TS3VLFHANCNFSM4WRQVS4Q .
Comment From: JJBRT
Please take a look at my new article that explain how to export all modules to all modules at runtime in Java 16 and later without using any JVM parameter
Comment From: snicoll
I think this issue has run its course and is no longer actionable.