一、Spring的Bean属性的依赖注入
为一个Bean设置属性,有三种注入属性的方式:
1)接口注入
2)构造器注入
3)setter方法注入
详细介绍如下:
接口注入:
public interface Injection{
public void injectName(String name);
}
为对象注入name属性
public class User implements Injection{
public void injectName(String name){
this.name = name;
}
private String name;
}
构造器注入:
public class User{
private String name;
public User(String name){
this.name = name;
}
}
setter方法注入:
public class User{
private String name;
public void setName(String name){
this.name = name;
}
}
☆:Spring框架支持构造器注入和setter方法注入。
Spring中属性的依赖注入:
第一种:构造器注入,通过 <constructor-arg> 元素完成注入
public class Car {
private String name;
private double price;
public Car(String name, double price){
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
<!-- 构造器注入 -->
<bean id="car" class="lsq.spring.c_di.Car">
<!-- 通过构造器参数,完成属性的注入 -->
<constructor-arg index="0" type="java.lang.String" value="奥迪"></constructor-arg>
<constructor-arg index="1" type="double" value="500000"></constructor-arg>
</bean>
第二种:setter方法注入, 通过<property> 元素完成注入
public class Car2 {
private String name;
private double price;
//注入属性时,只需要提供set方法
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car2 [name=" + name + ", price=" + price + "]";
}
}
<!-- setter方法注入 -->
<bean id="car2" class="lsq.spring.c_di.Car2">
<!-- 通过property元素完成属性注入 -->
<property name="name" value="JEEP"></property>
<property name="price" value="200000"></property>
</bean>
* 使用 <property> 元素 ref属性,引入另一个Bean对象,完成Bean之间注入
public class Employee {
private String name;
private Car2 car2;
public void setName(String name) {
this.name = name;
}
public void setCar2(Car2 car2) {
this.car2 = car2;
}
@Override
public String toString() {
return "Employee [name=" + name + ", car2=" + car2 + "]";
}
}
<bean id="employee" class="lsq.spring.c_di.Employee">
<property name="name" value="wade"></property>
<property name="car2" ref="car2"></property>
</bean>
以上逻辑的测试代码如下:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 完成依赖注入的测试
*
* @author lishanquan
*
*/
public class DITest {
@Test
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
@Test
public void demo2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
}
@Test
public void demo3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee employee = (Employee) applicationContext.getBean("employee");
System.out.println(employee);
}
}
二、* 名称空间 p的使用 (Spring2.5 新特性)
spring2.5版本 引入名称空间p, 简化属性注入的配置
p:<属性名>="xxx" 引入常量值
p:<属性名>-ref="xxx" 引用其它Bean对象
具体使用步骤:
1) 引入p名称空间
2)改写<property>注入为 p名称空间注入
修改为如下所示:
测试结果相同。
三、* spring3.0之后引入 spEL 表达式
1、完成对象之间注入
修改为
2、使用另一个Bean属性完成注入
<span style="white-space:pre"> </span><bean id="carInfo" class="cn.itcast.spring.e_di.CarInfo"></bean>
<bean id="car2_2" class="cn.itcast.spring.e_di.Car2">
<property name="name" value="#{carInfo.name}"></property>
</bean>
3、 使用另一个Bean方法完成注入
<bean id="carInfo" class="cn.itcast.spring.e_di.CarInfo"></bean>
<bean id="car2_2" class="cn.itcast.spring.e_di.Car2">
<property name="name" value="#{carInfo.name}"></property>
<property name="price" value="#{carInfo.caculatePrice()}"></property>
</bean>
四、集合属性的注入
Spring提供专门标签完成List、Set、Map、Properties等集合元素属性注入。
1) 注入List (数组)
import java.util.List;
public class CollectionBean {
private List hobbies;
public void setHobbies(List hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "CollectionBean [hobbies=" + hobbies + "]";
}
}
<!-- 集合属性注入 -->
<bean id="collectionBean" class="lsq.spring.c_di.CollectionBean">
<property name="hobbies">
<list>
<!-- <value>标签注入基本类型,<ref/>注入复杂类型 -->
<value>体育</value>
<value>音乐</value>
</list>
</property>
</bean>
2)注入Set
<property name="numbers">
<set>
<value>10</value>
<value>6</value>
<value>15</value>
</set>
</property>
3)注入Map
<property name="map">
<map>
<span style="white-space:pre"> </span><!-- 复杂类型 <entry key-ref="" value-ref=""></entry> -->
<entry key="name" value="wade"></entry>
<entry key="address" value="迈阿密"></entry>
</map>
</property>
4)注入Properties
* java.utils.Properties 类 继承 java.utils.HashTableProperties key和value都是String类型。
例如:
<property name="properties">
<props>
<prop key="company">NBA</prop>
<prop key="pnum">100</prop>
</props>
</property>
五、在Spring框架中引入多个XML配置文件
1、并列引入多个XML
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans1.xml", "beans2.xml");
2、引入总xml文件,在总xml文件引入 子xml文件
<span style="white-space:pre"> </span>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
<span style="white-space:pre"> </span>* 在applicationContext.xml 中
<import resource="classpath:bean1.xml"/>
<import resource="classpath:bean2.xml"/>
在开发中主要使用 第二种 , 将配置文件分离配置 便于维护管理