🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 属性的依赖注入 spring在通过IOC创建这个对象的时候,如果这个对象还有属性,就一并赋值.ID是在IOC的基础上进行对象的属性注入的.也就是说先有IOC,才能DI. ~~~ <bean id="user" class="com.like.serviceImpl.User "> <property name="name" value="jack"/> <property name="age" value="20"/> </bean> ~~~ ## ref属性 ~~~ <bean id="car" class="com.like.domain.Car"> <property name="name" value="兰博基尼"/> <property name="price" value="10000"/> </bean> <bean id="user" class="com.like.domain.User"> <property name="name" value="jack"/> <property name="age" value="20"/> <property name="car" ref="car"/> //ref针对的是对象类型 </bean> ~~~ ~~~ public class User { private String name; private Integer age; private Car car; //有Car的对象 @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } } ~~~ 结果: ``` User{name='jack', age=20, car=Car{name='兰博基尼', price=10000.0}} ``` ## 构造器依赖注入 条件是,必须有有参构造器. 配置: ~~~ <bean id="user" class="com.like.User"> <constructor-arg name="name" value="jack"/> //value针对的是基本类型和String类型 <constructor-arg name="age" value="20"/> </bean> ~~~ bean: ~~~ public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ~~~ ## P名称空间属性注入 条件:在配置文件中有P的名称空间,底层还是用的set方式. ~~~ xmlns:p="http://www.springframework.org/schema/p" //:后面的 p 是别名 ~~~ ~~~ <bean id="car" class="com.like.domain.Car"> <property name="name" value="兰博基尼"/> <property name="price" value="10000"/> </bean> <bean id="user" class="com.like.domain.User" p:name="jack" p:age="20" p:car-ref="car"></bean> ~~~ ## 复杂方式注入 ~~~ public class CollBean { private String[] strings; private List list; private Map map; private Properties properties; @Override public String toString() { return "CollBean{" + "strings=" + Arrays.toString(strings) + ", list=" + list + ", map=" + map + ", properties=" + properties + '}'; } public String[] getStrings() { return strings; } public void setStrings(String[] strings) { this.strings = strings; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } } ~~~ ~~~ <bean id="car" class="com.like.domain.Car"> <property name="name" value="兰博基尼"/> <property name="price" value="200"/> </bean> <bean id="collBean" class="com.like.domain.CollBean"> <property name="strings"> <list> <value>a</value> <value>b</value> <value>c</value> </list> </property> <property name="list"> <list> <value>1</value> <value>2</value> <ref bean="car"/> </list> </property> <property name="map"> <map> <entry key="key1" value="a"/> <entry key="key2" value="b"/> <entry key="key2" value-ref="car"/> </map> </property> <property name="properties"> <props> <prop key="hibernate.username">root</prop> <prop key="hibernate.password">123</prop> </props> </property> </bean> ~~~ 结果: ``` CollBean{strings=[a, b, c], list=[1, 2, Car{name='兰博基尼', price=200.0}], map={key1=a, key2=Car{name='兰博基尼', price=200.0}}, properties={hibernate.password=123, hibernate.username=root}} ```