Spring 依赖注入
所谓的依赖注入,就是属性不用 new 创建对象,通过配置文件的配置将 Spring 容器里面的对象注入给对应的属性。
(1)set 方法注入
类代码:
1 package cn.mgy.service; 2 3 import java.util.Date; 4 5 public class CustomerService { 6 7 private int age; 8 private String name; 9 private Date birthDate; 10 11 12 13 public void setAge(int age) { 14 this.age = age; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public void setBirthDate(Date birthDate) { 22 this.birthDate = birthDate; 23 } 24 25 public void reigster(){ 26 System.out.println("姓名:"+name); 27 System.out.println("年龄:"+age); 28 System.out.println("生日:"+birthDate); 29 } 30 31 }
配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> 5 <bean name="now" class="java.util.Date"></bean> 6 <bean name="customerService" class="cn.mgy.service.CustomerService"> 7 <!-- 一个property标签匹配一个set方法 --> 8 <!-- 9 只要是标量类型,可以使用value设置, 10 注意:所谓的标量类型就是基础数据类型(以及包装类)+String类型 11 如果非标量类型,必须使用ref引用对象 12 --> 13 <property name="name" value="张三"></property> 14 <property name="age" value="15"></property> 15 <property name="birthDate" ref="now"></property> 16 </bean> 17 </beans>
(2)构造方法注入
类的实现:
1 package cn.mgy.service; 2 3 import java.util.Date; 4 5 public class CustomerService { 6 7 private int age; 8 private String name; 9 private Date birthDate; 10 11 /** 12 * 声明一个有参数的构造方法 13 * @param age 14 * @param name 15 * @param birthDate 16 */ 17 public CustomerService(int age, String name, Date birthDate) { 18 super(); 19 this.age = age; 20 this.name = name; 21 this.birthDate = birthDate; 22 } 23 24 public void reigster(){ 25 System.out.println("姓名:"+name); 26 System.out.println("年龄:"+age); 27 System.out.println("生日:"+birthDate); 28 } 29 30 }
对应配置文件配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> 5 <bean name="now" class="java.util.Date"></bean> 6 <bean name="customerService" class="cn.mgy.service.CustomerService"> 7 <!-- 8 构造函数每一个参数:对应一个constructor-arg标签 9 name:指定参数名 10 value:指定注入的值 11 ref:指定引用的对象 12 index:指定参数的位置 13 type:指定参数的数据库类型,默认使用参数声明的类型【可以忽略】 14 15 --> 16 <constructor-arg name="age" value="15"></constructor-arg> 17 <constructor-arg name="name" type="java.lang.String" value="张三"></constructor-arg> 18 19 <constructor-arg name="birthDate" ref="now"></constructor-arg> 20 21 <!-- 通过位置指定参数 --> 22 <!-- <constructor-arg index="0" value="15"></constructor-arg> --> 23 <!-- <constructor-arg index="1" value="张三"></constructor-arg> --> 24 <!-- <constructor-arg index="2" ref="now"></constructor-arg> --> 25 26 </bean> 27 </beans>
(3)使用 p 标签注入属性
类代码:
1 package cn.mgy.service; 2 3 import java.util.Date; 4 5 public class CustomerService { 6 7 private int age; 8 private String name; 9 private Date birthDate; 10 11 public void setAge(int age) { 12 this.age = age; 13 } 14 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 public void setBirthDate(Date birthDate) { 20 this.birthDate = birthDate; 21 } 22 23 public void reigster(){ 24 System.out.println("姓名:"+name); 25 System.out.println("年龄:"+age); 26 System.out.println("生日:"+birthDate); 27 } 28 29 }
配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> 6 <bean name="now" class="java.util.Date"></bean> 7 <bean name="customerService" class="cn.mgy.service.CustomerService" p:name="张三" p:age="15" p:birthDate-ref="now"> 8 </bean> 9 </beans>
(4)注入集合数据:
在处理数据中,有标量类型 = 基础数据类型以及包装类 + String --- value 属性
也有 Spring 容器里面的对象 --- ref 属性
还有很多数据 JDK 内置的数据结构:
① 键值对 Map <map>、Properties <prop>
② 数组 <array>
③ 集合 Set <set>、List <list>
类代码:
1 package cn.mgy.service; 2 3 import java.util.Arrays; 4 import java.util.List; 5 import java.util.Map; 6 import java.util.Properties; 7 import java.util.Set; 8 9 public class CustomerService { 10 11 private String[] myStrs; 12 private List<String> myList; 13 private Set<String> mySet; 14 private Map<String,Object> myMap; 15 private Properties myProps; 16 17 public void setMyStrs(String[] myStrs) { 18 this.myStrs = myStrs; 19 } 20 21 public void setMyList(List<String> myList) { 22 this.myList = myList; 23 } 24 25 public void setMySet(Set<String> mySet) { 26 this.mySet = mySet; 27 } 28 29 public void setMyMap(Map<String, Object> myMap) { 30 this.myMap = myMap; 31 } 32 33 public void setMyProps(Properties myProps) { 34 this.myProps = myProps; 35 } 36 37 public void reigster(){ 38 System.out.println(Arrays.toString(myStrs)); 39 System.out.println(myList); 40 System.out.println(mySet); 41 System.out.println(myMap); 42 System.out.println(myProps); 43 } 44 45 }
配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> 5 <bean name="now" class="java.util.Date"></bean> 6 <bean name="customerService" class="cn.mgy.service.CustomerService"> 7 8 <property name="myStrs"> 9 <array> 10 <value>AAA</value> 11 <value>BBB</value> 12 <value>CCC</value> 13 </array> 14 </property> 15 <property name="mySet"> 16 <set> 17 <value>AAA</value> 18 <value>BBB</value> 19 <value>CCC</value> 20 </set> 21 </property> 22 <property name="myList"> 23 <list > 24 <value>AAA</value> 25 <value>BBB</value> 26 <value>CCC</value> 27 </list> 28 </property> 29 <property name="myMap"> 30 <map> 31 <entry key="id" value="1" /> 32 <entry key="name" value="张三"></entry> 33 <entry key="birthDate" value-ref="now"></entry> 34 </map> 35 </property> 36 <property name="myProps"> 37 <props> 38 <prop key="id" >1</prop> 39 <prop key="name">张三</prop> 40 41 </props> 42 </property> 43 </bean> 44 </beans>
注意事项:集合与数组的标签可以互相调换使用的。但是不建议调换使用。