使用@autowired时私有字段出错

u3r8eeie  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(441)

在更改变量的可见性时,使用spring时出错。
我有这个代码(它工作得很好)

  1. @Component
  2. public class TennisCoach implements Coach {
  3. @Autowired
  4. public FortuneService fortuneService;
  5. @Override
  6. public String getDailyWorkout() {
  7. return "Do tennis stuff!";
  8. }
  9. @Override
  10. public String getFortune() {
  11. return fortuneService.getFortune();
  12. }
  13. }

( FortunateService 是一个接口,我有一个类 HappyFortunateService 它实现了它,我用 @Component 以及)
用这个主要方法

  1. public static void main(String[] args) {
  2. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  3. Coach annotatedCoach = context.getBean("tennisCoach", Coach.class);
  4. System.out.println(annotatedCoach.getDailyWorkout());
  5. System.out.println(annotatedCoach.getFortune());
  6. context.close();
  7. }

当我打开 TennisCoach 班级 fortunateService 可变可见性 private ,我得到这个错误:

  1. org.springframework.context.support.AbstractApplicationContext refresh
  2. WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tennisCoach': Injection of autowired dependencies failed; nested exception is java.lang.reflect.InaccessibleObjectException: Unable to make field private me.davichete.springproject.FortuneService me.davichete.springproject.TennisCoach.fortuneService accessible: module spring_project does not "opens me.davichete.springproject" to module spring.core
  3. Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tennisCoach': Injection of autowired dependencies failed; nested exception is java.lang.reflect.InaccessibleObjectException: Unable to make field private me.davichete.springproject.FortuneService me.davichete.springproject.TennisCoach.fortuneService accessible: module spring_project does not "opens me.davichete.springproject" to module spring.core
  4. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
  5. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
  6. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
  7. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
  8. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
  9. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
  10. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
  11. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
  12. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
  13. at spring.context@5.2.9.RELEASE/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
  14. at spring.context@5.2.9.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
  15. at spring.context@5.2.9.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
  16. at spring.context@5.2.9.RELEASE/org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
  17. at spring_project/me.davichete.springproject.HelloSpringApp.main(HelloSpringApp.java:8)
  18. Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private me.davichete.springproject.FortuneService me.davichete.springproject.TennisCoach.fortuneService accessible: module spring_project does not "opens me.davichete.springproject" to module spring.core
  19. at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:361)
  20. at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:301)
  21. at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
  22. at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
  23. at spring.core@5.2.9.RELEASE/org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:782)
  24. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:667)
  25. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
  26. at spring.beans@5.2.9.RELEASE/org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
  27. ... 13 more

有人知道为什么吗?spring版本:5.2.9 java se15
编辑:我的项目是将springjar文件添加到modulepath的,我没有使用maven。
我的module-info.java如下所示:

  1. module spring_project {
  2. requires spring.context;
  3. requires java.logging;
  4. requires spring.beans;
  5. exports me.davichete.springproject;
  6. }

编辑:我在删除module-info.java时测试了它,现在它似乎工作了,可能与@menelaos的答案有关

vhipe2zx

vhipe2zx1#

尝试添加一个构造函数并使您的字段成为final

如果您正在使用 Lambok ,可以执行以下操作:
@RequiredArgsConstructor 在您的类上-它将为所有最终变量生成一个带参数的构造函数。
将字段改为final,并去掉@autowired。
spring将负责为final字段注入值。
如果不使用lambok,则需要手动添加构造函数。
参见构造函数注入示例@https://stackoverflow.com/a/55473346/1688441

java 9模块

还可以看一下以下内容:如何解决Java9上的不可访问对象异常(“无法使{member}可访问:模块{a}没有向{b}打开{package}”)?
您的错误是由于Java9中的限制和更改造成的。

相关问题