情况:我的课堂上注明了@Autowired:
public class MyClass {
@Autowired
protected MyAutoWiredBean myAutowiredBean;
}
有没有可能将这个bean连接成可选的,即如果在某个配置文件中定义了这样的bean - 连接它,但是如果没有定义这样的bean - 只需继续工作而不抛弃:
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.
答案 0 :(得分:19)
答案 1 :(得分:5)
您可以设置所需的属性,如:
@Autowired(required=false)