java 如何将 Spring @Autowired required 属性设置为 false 以进行测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3279868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to turn Spring @Autowired required property to false for test?
提问by nodje
I've been using the @Required annotation up to now to ensure DI of my beans in a Spring configured application.
到目前为止,我一直在使用 @Required 注释来确保在 Spring 配置的应用程序中我的 bean 的 DI。
To enable the annotation, you need to declare a RequiredAnnotationBeanPostProcessor bean in your configuration.
要启用注释,您需要在配置中声明一个 RequiredAnnotationBeanPostProcessor bean。
In your test configuration you just don't declare it, so that if some beans are not needed you don't have to have them in your config.
在您的测试配置中,您只是不声明它,这样如果不需要某些 bean,您就不必在配置中包含它们。
I want to switch to less XML and use @Autowired annotation, but it is by default required=true, which is fine for the runtime configuration.
我想切换到较少的 XML 并使用 @Autowired 注释,但默认情况下它是 required=true,这对于运行时配置很好。
But I need @Autowired to be required=false for testing purpose only - while keeping it required for runtime.
但我需要 @Autowired 为 required=false 仅用于测试目的 - 同时保持运行时所需。
Is that possible at all? I can't find a way to declaratively turn the required property to false.
这可能吗?我找不到以声明方式将所需属性转换为 false 的方法。
cheers
干杯
回答by Peter
You probably solved it already but this trick might be useful for others.
你可能已经解决了,但这个技巧可能对其他人有用。
As far as I understood without context:annotation-drivenbeing present @Autowired annotations should not be processed but this is clearly not the case so I might misunderstood something.
据我所知,没有上下文:注释驱动的存在@Autowired 注释不应该被处理,但这显然不是这种情况,所以我可能会误解一些东西。
However, I needed a quick solution... This somewhat dirty trick negates required value for all classes making optional what was required before. Adding it to my test contextsolved my problem but it is useful only if all autowirings are required in your business classes.
但是,我需要一个快速的解决方案......这个有点肮脏的技巧否定了所有类的必需值,使以前需要的可选值。将它添加到我的测试上下文中解决了我的问题,但只有在您的业务类中需要所有自动装配时,它才有用。
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
<property name="requiredParameterValue" value="false" />
</bean>
回答by tcheng09
I worked out a solution that works for JavaConfig configurations
我制定了一个适用于 JavaConfig 配置的解决方案
@ContextConfiguration(initializers={DisableAutowireRequireInitializer.class})
public class TestCase {
// Some tests
}
public class DisableAutowireRequireInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// Register the AutowiredAnnotationBeanPostProcessor while initalizing
// the context so we get there before any @Autowire resolution happens
// We set the "requiredParameterValue" so that @Autowire fields are not
// required to be resolved. Very useful for a test context
GenericApplicationContext ctx = (GenericApplicationContext) applicationContext;
ctx.registerBeanDefinition(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME,
BeanDefinitionBuilder
.rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class)
.addPropertyValue("requiredParameterValue", false)
.getBeanDefinition());
}
}
回答by skaffman
You can use the same technique as you did with @Required- don't register the AutowiredAnnotationBeanPostProcessorin your test context, but leave it in your live context.
您可以使用与之前相同的技术@Required- 不要AutowiredAnnotationBeanPostProcessor在您的测试上下文中注册,而是将它留在您的实时上下文中。
This is usually registered by adding <context:annotation-driven/>, rather than being declared manually.
这通常通过添加注册<context:annotation-driven/>,而不是手动声明。

