views:

31

answers:

1

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

A: 

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.

skaffman
Wait a minute, if you don't add the <context:annotation-config />, Spring doesn't even detect the @Autowired annotation, right?What I'd like, ideally, but I'm sure many have faced the problem, is to have Spring load the <context:annotation-config />, i-e detect annotations, but turn the default @Autowired(required = true) to false, so that I don't have to inject dependencies that are required at runtime but not useful, or even not welcome, in test mode.
nodje
@nodje: Fair enough, but that's not how it works, I'm afraid.
skaffman