简体   繁体   中英

How to turn Spring @Autowired required property to false for test?

I've been using the @Required annotation up to now to ensure DI of my beans in a Spring configured application.

To enable the annotation, you need to declare a RequiredAnnotationBeanPostProcessor bean in your configuration.

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.

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.

But I need @Autowired to be required=false for testing purpose only - while keeping it required for runtime.

Is that possible at all? I can't find a way to declaratively turn the required property to false.

cheers

You probably solved it already but this trick might be useful for others.

As far as I understood without context:annotation-driven being present @Autowired annotations should not be processed but this is clearly not the case so I might misunderstood something.

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 context solved 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>

I worked out a solution that works for JavaConfig configurations

@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());

    }

}

You can use the same technique as you did with @Required - don't register the AutowiredAnnotationBeanPostProcessor in your test context, but leave it in your live context.

This is usually registered by adding <context:annotation-driven/> , rather than being declared manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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