本次介绍spring的依赖注入与属性注入
-
DI(依赖注入)Dependency Injection
依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!
* 例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来!
以后使用spring都是使用依赖注入,本次使用service与dao层来演示。
dao
package dao;
public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("udao.say");
}
}
service
package service;
import dao.UserDao;
import test.Demo1;
public class UserServiceImpl implements UserService{
//udao为依赖注入使用,必须提供setter方法,不然spring无法写入
private UserDao udao;
public void setUdao(UserDao udao) {
this.udao = udao;
}
@Override
public void usDemo1() {
new Demo1().demo1();
// System.out.println("demo1");
}
@Override
public void say() {
udao.say();
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 -->
<bean id="userSerivce" class="service.UserServiceImpl"/>
<!-- 演示依赖注入 -->
<bean id="userDaoImpl" class="dao.UserDaoImpl"/>
<bean id="userServiceImpl" class="service.UserServiceImpl">
<!-- property 中属性不要填写value是字符串,如果依赖注入需要填写ref属性 -->
<property name="udao" ref="userDaoImpl"></property>
</bean>
</beans>
测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
/**
* 演示依赖注入DI(Dependent Injection)
* 比如service需要调用dao才能实现功能就叫做service以来dao,以前调用时需要自己new dao使用,
* 交给spring管理的时候就可以动态的将依赖对象注入并使用该属性的值
* @author Administrator
*
*/
public class Demo5 {
@Test
public void demo1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userServiceImpl");
us.say();
}
}
-
属性注入
属性注入又分为两种:构造函数注入与属性setter注入
- 构造函数注入需要在bean中提供有参构造函数
package bean;
public class Car {
private String name;
private Double price;
public Car() {}
public Car(String name, Double price) {
this.name = name;
this.price = 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 + "]";
}
}
然后在srping配置文件(applicationContext.xml)中设置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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="bean.Car">
<!-- 使用name赋值 -->
<constructor-arg name="name" value="奔驰" />
<constructor-arg name="price" value="500000" />
<!-- 使用index赋值
<constructor-arg index="0" value="宝马"/>
<constructor-arg index="1" value="600000"/>
-->
</bean>
</beans>
测试类,这样就在spring管理创建car类的时候注入name与price了
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Car;
/**
* 演示构造函数注入的方法
* @author Administrator
*
*/
public class Demo4 {
@Test
public void demo1() {
ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) aContext.getBean("car");
System.out.println(car);
}
}
- 属性setter注入
bean无需修改提供setter方法就可以了
主要在spring配置文件中设置bean中constructor-arg标签改为property标签即可,bean类无需修改。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 属性setter注入 -->
<bean id="car1" class="bean.Car">
<!-- 使用property赋值 -->
<property name="name" value="奔驰"/>
<property name="price" value="500000"/>
</bean>
</beans>
测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Car;
/**
* 演示属性setter注入的方法
* @author Administrator
*
*/
public class Demo6 {
@Test
public void demo1() {
ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) aContext.getBean("car1");
System.out.println(car);
}
}
- spring配置文件中关联使用其他bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 属性setter注入 -->
<bean id="car1" class="bean.Car">
<!-- 使用property赋值 -->
<property name="name" value="奔驰"/>
<property name="price" value="500000"/>
</bean>
<!--
演示spring配置文件中关联使用其他bean
-->
<bean id="user" class="bean.User">
<property name="car" ref="car1"></property>####
<property name="user" value="alex"></property>
<property name="password" value="pass1"></property>
</bean>
</beans>
测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.User;
import service.UserService;
/**
* spring的测试代码
* @author Administrator
*
*/
public class Demo2 {
/*
* 演示spring配置文件中关联使用其他bean
* bean中需要有需要注入bean的成员变量并提供setter方法
* applicationContext.xml中需要用ref属性来注入
*/
@Test
public void demo2() {
//获得ApplicationContext,如需关闭context,需要将变量的接口类型换位ClassPathXmlApplicationContext实现类
ApplicationContext aContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得UserService
User user = (User) aContext.getBean("user");
//执行方法
System.out.println(user);
}
}
输出结果
-
使用spEL(Spring Expression Language) 注入
SpEL使用#{…}作为定界符,所有在大框号中的字符都将被认为是SpEL.
具体使用可以参考:https://blog.csdn.net/gudong2945/article/details/7330642#
以下是简单的演示代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--
演示spEL(Spring Expression Language)注入
-->
<bean id="car2" class="bean.Car">
<!-- 赋值字符串 -->
<property name="name" value="#{'阿斯顿马丁'}"/>
<!-- 赋值数字 -->
<property name="price" value="#{2222222}"/>
</bean>
</beans>
测试代码:
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Car;
/**
* 演示spEL的注入
* @author Administrator
*
*/
public class Demo7 {
@Test
public void demo1() {
ApplicationContext acontext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car2 = (Car) acontext.getBean("car2");
System.out.println(car2);
}
}
输出结果:
-
spring 数组,集合(List,Set,Map),Properties等的注入
测试bean
package bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* 演示spring 数组,集合(List,Set,Map),Properties等的注入
* @author Administrator
*
*/
public class TestBean {
//数组
private String[] args;
public void setArgs(String[] args) {
this.args = args;
}
//集合List
private List list;
public void setList(List list) {
this.list = list;
}
//集合Set
private Set set;
public void setSet(Set set) {
this.set = set;
}
//集合Map
private Map map;
public void setMap(Map map) {
this.map = map;
}
//map key为对象
private Map<User, String> map1;
public void setMap1(Map<User, String> map1) {
this.map1 = map1;
}
//Properties
private Properties pro;
public void setPro(Properties pro) {
this.pro = pro;
}
@Override
public String toString() {
return "TestBean [\nargs=" + Arrays.toString(args) + ", \nlist=" + list + ", \nset=" + set + ", \nmap=" + map + ", \nmap1=" + map1 + ", \npro="
+ pro + "]";
}
}
用户
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!--
演示spring配置文件中关联使用其他bean
-->
<bean id="user" class="bean.User">
<property name="car" ref="car1"></property>
<property name="user" value="alex"></property>
<property name="password" value="pass1"></property>
</bean>
<!-- 演示spring 数组,集合(List,Set,Map),Properties等的注入 -->
<bean name="TestBean" class="bean.TestBean">
<!-- 数组 -->
<property name="args">
<list>
<value>val1</value>
<value>val2</value>
<value>val3</value>
</list>
</property>
<!-- 列表 -->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!-- Set -->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!-- Map -->
<property name="map">
<map>
<entry key="key1" value="val1"/>
<entry key="key2" value="val2"/>
<entry key="key3" value="val3"/>
</map>
</property>
<!-- Map key为对象 -->
<property name="map1">
<map>
<entry key-ref="user" value="Map key为对象"/>
</map>
</property>
<!-- Properties -->
<property name="pro">
<props>
<prop key="pkey1">pval1</prop>
<prop key="pkey2">pval2</prop>
<prop key="pkey3">pval3</prop>
</props>
</property>
</bean>
</beans>
测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.TestBean;
/**
*
* 演示spring 数组,集合(List,Set,Map),Properties等的注入
* @author Administrator
*
*/
public class Demo8 {
@Test
public void demo1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TestBean tb = (TestBean) ac.getBean("TestBean");
System.out.println(tb);
}
}
输出结果: