Question on Spring 3 autoscan and required annotation
I have a class annotated with spring Component
e.g:
@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
}
I use autoscan to detect beans instead of declaring them all in the bean conf file.
I getorg.springframework.beans.factory.BeanInitializationException: Property 'otherClass' is required for bean 'ImportantClass'
My quest开发者_如何学编程ion is the following: In autoscan, doesn't Spring make sure that all the required properties are injected?
If I remove the@Required
it works, but I am not sure I understand Spring's behavior on this.
Any input is welcome.
Thanks
@Autowired
has required
set to true, so you don't need @Required
You don't need @Requried
with annotation-based injection. It is meant to be used with xml configuration, to denote that a property must be set in the xml.
精彩评论