有关Spring 3自动扫描和必需注释的问题

问题描述

| 我有一门课用春天的
Component
注释,例如:
@Component
public class ImportantClass{

@Autowired
private DependentClassIF otherClass;

//getters setters

@required 
public void setotherClass(DependentClassIF c){
this.otherClass = c;    
}
public interface DependentClassIF {
    //methods here
}  
 @Component
 public class DependentClass implements DependentClassIF {
    //implementation here
 }
我使用自动扫描来检测bean,而不是在bean conf文件中全部声明它们。 我懂了   org.springframework.beans.factory.BeanInitializationException:   必须使用属性“'otherClass \”   豆\'ImportantClass \' 我的问题如下: 在自动扫描中,Spring不能确保注入所有必需的属性吗? 如果我删除ѭ3,它会起作用,但是我不确定我是否了解Spring在此方面的行为。 欢迎任何输入。 谢谢     

解决方法

        
@Autowired
required
设置为true,因此您不需要
@Required
基于注解的注入不需要ѭ7。它旨在与xml配置一起使用,以表示必须在xml中设置属性。