依赖注入的方式:
1.构造函数依赖注入
2.setter方法依赖注入
p名称空间的使用
Spring2.5 版本,引入p名称空间,作用简化 setter方法注入的配置
第一步: 在xml配置文件中,配置p名称空间
xmlns:p = “http://www.springframework.org/schema/p”
第二步: 通过p名称空间语法,简化setter方式属性注入
p:<属性名>=“xxx” 引入常量值
p:<属性名>-ref=“xxx” 引用其它Bean对象
改造为
2) SpEL 表达式使用
Spring Expression Language Spring表达式语言, 从3.0版本开始提供
语法格式 #{…} 主要用于spring配置文件
引入对象, 引用对象属性, 引用对象方法
代码示例:
Car.java
package com.my.di;
//轿车 (构造器 依赖注入)
public class Car {
private String name;
private double price ;
// 在构造Car对象时,将name和price 属性注入car对象
public Car(String name, double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
Car2.java
package com.my.di;
//轿车 通过setter方法完成属性注入
public class Car2 {
private String name;
private double price ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
Employee.java
package com.my.di;
public class Employee {
private Car car ;
private String name;
public void setCar(Car car) {
this.car = car;
}
public void setName(String name) {
this.name = name;
}
public Employee(Car car, String name) {
super();
this.car = car;
this.name = name;
}
public Employee() {
}
@Override
public String toString() {
return "Employee [car=" + car + ", name=" + name + "]";
}
}
applicationContext.xml
?xml version="1.0" encoding="UTF-8"?>
Test.java
package com.my.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
// 测试构造函数注入
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
@org.junit.Test
// 测试setter方法注入
public void demo2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
}
@org.junit.Test
// 测试setter方法注入 (复杂属性)
public void demo3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee employee = (Employee) applicationContext.getBean("employee");
System.out.println(employee);
}
@org.junit.Test
// 测试setter方法注入 (p名称空间)
public void demo4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee employee2 = (Employee) applicationContext.getBean("employee2");
System.out.println(employee2);
}
@org.junit.Test
// 测试setter方法注入 (spEL使用)
public void demo5(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 mycar = (Car2) applicationContext.getBean("mycar");
System.out.println(mycar);
Employee employee = (Employee) applicationContext.getBean("myemployee");
System.out.println(employee);
}
}
运行结果:
集合属性 的注入
Spring 提供集合对象注入 四个标签
代码示例:
CollectionBean.java
package com.my.collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.my.di.Car;
//注入 集合类型 属性
public class CollectionBean {
private List hobbies;
private List cars;
private Set scores;
private Map map;
private Properties properties;
public void setHobbies(List hobbies) {
this.hobbies = hobbies;
}
public void setCars(List cars) {
this.cars = cars;
}
public void setScores(Set scores) {
this.scores = scores;
}
@Override
public String toString() {
return "CollectionBean [ hobbies=" + hobbies + ",\n cars=" + cars
+ ", scores=" + scores + ",\n map=" + map + ",\n properties="
+ properties + " ]";
}
public void setMap(Map map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
applicationContext.xml
音乐
体育
100
90
北京
20
Test.java
package com.my.collection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean bean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(bean);
}
}