2011-10-07 32 views
7

形势 @Autowired属性:我有我类属性与@Autowired注解:春:怎么不理,如果豆没有定义

public class MyClass { 
    @Autowired 
    protected MyAutoWiredBean myAutowiredBean; 
} 

是否有可能以制作布线这个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. 

回答

13

你试过:

@Autowired(required=false) 

Javadoc

声明是否需要注解的依赖。默认为true

+0

是的,是完美的作品,谢谢。 – dim1902