Spring支持4种依赖检查:默认的是none
-
- none – No dependency checking.
- simple – If any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, UnsatisfiedDependencyException will be thrown.
- objects – If any properties of object type have not been set, UnsatisfiedDependencyException will be thrown.
all – If any properties of any type have not been set, an UnsatisfiedDependencyException will be thrown.
举个例子:
public class Customer { private Person person; private int type; private String action; //getter and setter methods}
public class Person { private String name; private String address; private int age; //getter and setter methods }
1. none dependency checking
2. simple dependency checking
注意此处type字段故意没有设置,这样会出现UnsatisfiedDependencyException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'type': Set this property value or disable dependency checking for this bean.
3. objects dependency checking
此处故意没有设置”person“属性,会出现UnsatisfiedDependencyException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerBean' defined in class path resource [config/Spring-Customer.xml]: Unsatisfied dependency expressed through bean property 'person': Set this property value or disable dependency checking for this bean.
4. all dependency checking
Global default dependency checking:
default-dependency-check="all"