简体   繁体   English

删除bean后@Autowired(required = false)失败

[英]@Autowired(required = false) fails after removing bean

Steps to reproduce on https://github.com/nowszy94/Autowired-null https://github.com/nowszy94/Autowired-null上重现的步骤

After removing bean from Spring context with BeanDefinitionRegistry:removeBeanDefinition method, @Autowired(required = false) spring throws error: 使用BeanDefinitionRegistry:removeBeanDefinition方法从Spring上下文中删除bean后,@ @Autowired(required = false) spring throws error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloService' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:508) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$200(AutowiredAnnotationBeanPostProcessor.java:115) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:538) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]

Autowiring occurs at bean creation. 自动装配发生在bean创建时。 If you did not have a helloService bean defined, then your spring application would not throw an exception when attempting to satisfy that dependency during bean creation. 如果您没有定义helloService bean,那么在bean创建期间尝试满足该依赖项时,spring应用程序不会抛出异常。 In your example you are removing the helloService bean from the application context after it has already been wired into your HelloController bean. 在您的示例中,您将从应用程序上下文中将helloService bean连接到HelloController bean之后将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM