概述
分享我的一个问题解决:
在module-info-java中添加相关exports即可
module test { requires spring.context; requires junit; exports test; exports POJO; exports service; }
报错提示: /Users/wgx/Library/Java/JavaVirtualMachines/openjdk-18.0.2.1/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59750:/Applications/IntelliJ IDEA.app/Contents/bin --add-reads test=ALL-UNNAMED --add-opens test/test=ALL-UNNAMED --add-modules test -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Users/wgx/Downloads/mysql-connector-java-8.0.30/mysql-connector-java-8.0.30.jar:/Users/wgx/.m2/repository/org/springframework/spring-aop/5.3.18/spring-aop-5.3.18.jar:/Users/wgx/.m2/repository/org/springframework/spring-beans/5.3.18/spring-beans-5.3.18.jar:/Users/wgx/.m2/repository/org/springframework/spring-core/5.3.18/spring-core-5.3.18.jar:/Users/wgx/.m2/repository/org/springframework/spring-jcl/5.3.18/spring-jcl-5.3.18.jar:/Users/wgx/.m2/repository/org/springframework/spring-expression/5.3.18/spring-expression-5.3.18.jar:/Users/wgx/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar -p /Users/wgx/IdeaProjects/GarsonSpring/target/classes:/Users/wgx/.m2/repository/junit/junit/4.13.2/junit-4.13.2.jar:/Users/wgx/.m2/repository/org/springframework/spring-context/5.3.18/spring-context-5.3.18.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 test.SpringTest,testRuns
调用StudentService无参构造
10月 02, 2022 4:06:45 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentService' defined in class path resource [bean2.xml]: Cannot resolve reference to bean 'StudentDao' while setting bean property 'studentDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentDao' defined in class path resource [bean2.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [dao.StudentDaoImpl]: Is the constructor accessible?; nested exception is java.lang.IllegalAccessException: class org.springframework.beans.BeanUtils cannot access class dao.StudentDaoImpl (in module test) because module test does not export dao to unnamed module @34b7ac2f
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentService' defined in class path resource [bean2.xml]: Cannot resolve reference to bean 'StudentDao' while setting bean property 'studentDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentDao' defined in class path resource [bean2.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [dao.StudentDaoImpl]: Is the constructor accessible?; nested exception is java.lang.IllegalAccessException: class org.springframework.beans.BeanUtils cannot access class dao.StudentDaoImpl (in module test) because module test does not export dao to unnamed module @34b7ac2f
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at spring.context@5.3.18/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at spring.context@5.3.18/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at spring.context@5.3.18/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at spring.context@5.3.18/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at test/test.SpringTest.testRuns(SpringTest.java:26)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at junit@4.13.2/org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at junit@4.13.2/org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at junit@4.13.2/org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at junit@4.13.2/org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at junit@4.13.2/org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at junit@4.13.2/org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at junit@4.13.2/org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at junit@4.13.2/org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at junit@4.13.2/org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at junit@4.13.2/org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at junit@4.13.2/org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at junit@4.13.2/org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at junit@4.13.2/org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at junit@4.13.2/org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at junit@4.13.2/org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at junit@4.13.2/org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at junit@4.13.2/org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StudentDao' defined in class path resource [bean2.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [dao.StudentDaoImpl]: Is the constructor accessible?; nested exception is java.lang.IllegalAccessException: class org.springframework.beans.BeanUtils cannot access class dao.StudentDaoImpl (in module test) because module test does not export dao to unnamed module @34b7ac2f
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
... 40 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [dao.StudentDaoImpl]: Is the constructor accessible?; nested exception is java.lang.IllegalAccessException: class org.springframework.beans.BeanUtils cannot access class dao.StudentDaoImpl (in module test) because module test does not export dao to unnamed module @34b7ac2f
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:218)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1326)
... 48 more
Caused by: java.lang.IllegalAccessException: class org.springframework.beans.BeanUtils cannot access class dao.StudentDaoImpl (in module test) because module test does not export dao to unnamed module @34b7ac2f
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:394)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:492)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211)
... 50 more
进程已结束,退出代码255
最后
以上就是欢喜画板为你收集整理的JavaSpring中Exception encountered during context initialization - cancelling refresh attempt:的全部内容,希望文章能够帮你解决JavaSpring中Exception encountered during context initialization - cancelling refresh attempt:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复