2011-06-13 40 views
4

我有一个类注释与春天Component例如:问题上的弹簧3自动扫描和需要注解

@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的conf文件。
我得到

org.springframework.beans.factory.BeanInitializationException: Property 'otherClass' is required for bean 'ImportantClass'

我的问题是: 在自动扫描,并放不是春确保所有需要属性注入?
如果我删除了@Required它可行,但我不确定我是否理解Spring的行为。

欢迎任何输入。

感谢

回答

3

@Autowiredrequired设置为true,所以你不需要@Required

你不需要@Requried与基于注释的注入。它意味着与xml配置一起使用,以表示必须在xml中设置一个属性。

+0

你的意思是2个注释在同一个地方吗?如果我在'private DependentClassIF otherClass上执行'@Autowired @ Required';'我得到错误:'注释@Required不允许用于这个位置' – Cratylus 2011-06-13 10:25:27

+0

@ user384706看到更新 – Bozho 2011-06-13 10:37:12