依赖注入的方式:
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对象
<bean id="employee" class="cn.itcast.spring.e_di.Employee">
<property name="name" value="小明"></property>
<!-- 复杂类型对象, 可以通过 ref 属性引用目标bean的id -->
<property name="car" ref="car"></property>
</bean>
改造为
<!-- p名称空间 -->
<bean id="employee2"
class="cn.itcast.spring.e_di.Employee"
p:name="小丽" p:car-ref="car"></bean>
2) SpEL 表达式使用
Spring Expression Language Spring表达式语言, 从3.0版本开始提供
语法格式 #{..} 主要用于spring配置文件
引入对象, 引用对象属性, 引用对象方法
<!-- spEL 使用 -->
<bean id="mycar" class="cn.itcast.spring.e_di.Car2" p:carName="#{'qq'.toUpperCase()}"
p:price="#{car2.price}"></bean>
<bean id="myemployee" class="cn.itcast.spring.e_di.Employee" p:name="老张" p:car="#{car}"></bean>
代码示例:
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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 属性依赖注入 -->
<!-- 构造器注入 -->
<bean id="car" class="com.my.di.Car">
<!-- 构造函数中属性注入,使用 constructor-arg 标签完成注入 -->
<!-- 根据index 和 type 属性完成注入 -->
<constructor-arg index="0" type="java.lang.String" value="宝马"></constructor-arg>
<constructor-arg index="1" type="double" value="1000000"></constructor-arg>
</bean>
<!-- setter方法注入 -->
<bean id="car2" class="com.my.di.Car2">
<!-- 一个属性 ,对应一个property 元素 -->
<property name="name" value="保时捷"></property>
<property name="price" value="1500000"></property>
</bean>
<bean id="employee" class="com.my.di.Employee">
<property name="name" value="小明"></property>
<!-- 复杂类型对象, 可以通过 ref 属性引用目标bean的id -->
<property name="car" ref="car"></property>
</bean>
<!-- p名称空间 spring2.5 -->
<bean id="employee2" class="com.my.di.Employee" p:name="小丽" p:car-ref="car"></bean>
<!-- spEL 使用 spring3.0-->
<bean id="mycar" class="com.my.di.Car2" p:name="#{'qq'.toUpperCase()}" p:price="#{car2.price}"></bean>
<bean id="myemployee" class="com.my.di.Employee" p:name="老张" p:car="#{car}"></bean>
</beans>
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 提供集合对象注入 四个标签 <list> <set> <map> <props>
代码示例:
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<String> hobbies;
private List<Car> cars;
private Set<Integer> scores;
private Map<String, String> map;
private Properties properties;
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public void setScores(Set<Integer> 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<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 集合属性注入 -->
<bean id="collectionBean" class="com.my.collection.CollectionBean">
<property name="hobbies">
<list>
<value>音乐</value>
<value>体育</value>
</list>
</property>
<property name="cars">
<list>
<ref bean="car"/>
</list>
</property>
<property name="scores">
<set>
<value>100</value>
<value>90</value>
</set>
</property>
<property name="map">
<map>
<entry key="公司" value="百度"></entry>
<entry key="人数" value="100"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="城市">北京</prop>
<prop key="年龄">20</prop>
</props>
</property>
</bean>
</beans>
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);
}
}
运行结果:
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/12102.html