Spring: how to ignore @Autowired property if bean is not defined

The situation . I have a class with a property annotated with @Autowired:

public class MyClass { @Autowired protected MyAutoWiredBean myAutowiredBean; } 

Is it possible to make the wiring of this bean optional, i.e. if such a bean is defined in some configuration file - to post it, but if such a bean is not defined - just continue working without metaling:

 org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected MyAutoWiredBean...; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mypackage.MyAutoWiredBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 
+7
source share
2 answers

You tried:

 @Autowired(required=false) 

Javadoc :

Announces whether an annotated dependency is required. true default

+13
source

you can set the required attribute, for example:

 @Autowired(required=false) 

http://static.springsource.org/spring/docs/2.5.5/api/org/springframework/beans/factory/annotation/Autowired.html

+4
source

All Articles