依赖注入
依赖注入:
Dependency Injection
IOC的作用:
降低程序间的耦合(依赖关系)
依赖关系的管理:
以后都交给spring来维护
在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
依赖关系的维护:
就称之为依赖注入。
依赖注入:
能注入的数据:有三类
基本类型和String
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
注入的方式:有三种
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供(下篇内容再将)
构造函数注入:
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
name:用于指定给构造函数中指定名称的参数赋值 常用的
=============以上三个用于指定给构造函数中哪个参数赋值===============================
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
如果是经常变化的数据,不适合用构造方法的方式注入
set方法注入 更常用的方式
涉及的标签:property
出现的位置:bean标签的内部
标签的属性
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:
如果有某个成员必须有值,则获取对象是有可能set方法没有执行。
复杂类型的注入/集合类型的注入
用于给List结构集合注入的标签:
list array set
用于个Map结构集合注入的标签:
map props
结构相同,标签可以互换
1.创建maven项目,在pom.xml中添加spring依赖
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.cong</groupId> 8 <artifactId>spring-DI</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <packaging>jar</packaging> 12 <dependencies> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-context</artifactId> 16 <version>5.0.2.RELEASE</version> 17 </dependency> 18 </dependencies> 19 </project>
2.在java目录下创建com.cong.pojo.Car类
1 package com.cong.pojo; 2 3 public class Car { 4 private String brand; 5 private int price; 6 7 public Car(String brand, int price) { 8 this.brand = brand; 9 this.price = price; 10 } 11 12 @Override 13 public String toString() { 14 return "Car{" + 15 "brand='" + brand + '\'' + 16 ", price=" + price + 17 '}'; 18 } 19 }
3.在pojo目录下创建User类
1 package com.cong.pojo; 2 3 import java.util.Date; 4 import java.util.List; 5 import java.util.Map; 6 7 public class User { 8 private int id; 9 private String name; 10 private Date birthday; 11 private Car car; 12 private List<Car> cars; 13 private Map<String,Car> carMap; 14 15 @Override 16 public String toString() { 17 return "User{" + 18 "id=" + id + 19 ", name='" + name + '\'' + 20 ", birthday=" + birthday + 21 ", car=" + car + 22 ", cars=" + cars + 23 ", carMap=" + carMap + 24 '}'; 25 } 26 27 public void setBirthday(Date birthday) { 28 this.birthday = birthday; 29 } 30 public void setId(int id) { 31 this.id = id; 32 } 33 public void setName(String name) { 34 this.name = name; 35 } 36 public void setCar(Car car) { 37 this.car = car; 38 } 39 public void setCars(List<Car> cars) { 40 this.cars = cars; 41 } 42 43 public void setCarMap(Map<String, Car> carMap) { 44 this.carMap = carMap; 45 } 46 }
4.在pojo目录下创建Week类
1 package com.cong.pojo; 2 3 import java.util.List; 4 5 public class Week { 6 private List<String> days; 7 8 public void setDays(List<String> days) { 9 this.days = days; 10 } 11 12 @Override 13 public String toString() { 14 return "Week{" + 15 "days=" + days + 16 '}'; 17 } 18 }
5.在resources目录下创建bean.xml文件
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 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!-- spring中的依赖注入 7 依赖注入: 8 Dependency Injection 9 IOC的作用: 10 降低程序间的耦合(依赖关系) 11 依赖关系的管理: 12 以后都交给spring来维护 13 在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明 14 依赖关系的维护: 15 就称之为依赖注入。 16 依赖注入: 17 能注入的数据:有三类 18 基本类型和String 19 其他bean类型(在配置文件中或者注解配置过的bean) 20 复杂类型/集合类型 21 注入的方式:有三种 22 第一种:使用构造函数提供 23 第二种:使用set方法提供 24 第三种:使用注解提供(明天的内容) 25 --> 26 27 28 <!--构造函数注入: 29 使用的标签:constructor-arg 30 标签出现的位置:bean标签的内部 31 标签中的属性 32 type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型 33 index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始 34 name:用于指定给构造函数中指定名称的参数赋值 常用的 35 =============以上三个用于指定给构造函数中哪个参数赋值=============================== 36 value:用于提供基本类型和String类型的数据 37 ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象 38 39 优势: 40 在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。 41 弊端: 42 改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。 43 如果是经常变化的数据,不适合用构造方法的方式注入 44 --> 45 <bean id="benchi" class="com.cong.pojo.Car"> 46 <constructor-arg name="brand" value="奔驰"></constructor-arg> 47 <constructor-arg name="price" value="120000"></constructor-arg> 48 </bean> 49 <bean id="baoma" class="com.cong.pojo.Car"> 50 <constructor-arg type="java.lang.String" value="宝马"></constructor-arg> 51 <constructor-arg index="1" value="150000"></constructor-arg> 52 </bean> 53 <!-- 配置一个日期对象 --> 54 <bean id="now" class="java.util.Date"></bean> 55 <!-- set方法注入 更常用的方式 56 涉及的标签:property 57 出现的位置:bean标签的内部 58 标签的属性 59 name:用于指定注入时所调用的set方法名称 60 value:用于提供基本类型和String类型的数据 61 ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象 62 优势: 63 创建对象时没有明确的限制,可以直接使用默认构造函数 64 弊端: 65 如果有某个成员必须有值,则获取对象是有可能set方法没有执行。 66 --> 67 <bean id="cong" class="com.cong.pojo.User"> 68 <property name="id" value="1"></property> 69 <property name="name" value="cong"></property> 70 <property name="car" ref="baoma"></property> 71 <property name="birthday" ref="now"></property> 72 </bean> 73 <!-- 复杂类型的注入/集合类型的注入 74 用于给List结构集合注入的标签: 75 list array set 76 用于个Map结构集合注入的标签: 77 map props 78 结构相同,标签可以互换 79 --> 80 <bean id="week" class="com.cong.pojo.Week"> 81 <property name="days"> 82 <list> 83 <value>星期一</value> 84 <value>星期二</value> 85 <value>星期三</value> 86 <value>星期四</value> 87 <value>星期五</value> 88 <value>星期六</value> 89 <value>星期日</value> 90 </list> 91 </property> 92 </bean> 93 <bean id="rainbow" class="com.cong.pojo.User"> 94 <property name="cars"> 95 <!-- 下面的list可以换成array或者set --> 96 <list> 97 <ref bean="baoma"></ref> 98 <ref bean="benchi"></ref> 99 <bean class="com.cong.pojo.Car"> 100 <constructor-arg name="brand" value="本田"></constructor-arg> 101 <constructor-arg name="price" value="110000"></constructor-arg> 102 </bean> 103 </list> 104 </property> 105 </bean> 106 <bean id="Will" class="com.cong.pojo.User"> 107 <property name="carMap"> 108 <!-- 下面的map可以换成props --> 109 <map> 110 <entry key="baoma"> 111 <ref bean="baoma"></ref> 112 </entry> 113 <entry key="benchi"> 114 <ref bean="benchi"></ref> 115 </entry> 116 <entry key="bentian"> 117 <bean class="com.cong.pojo.Car"> 118 <constructor-arg name="brand" value="QQ"></constructor-arg> 119 <constructor-arg name="price" value="50000"></constructor-arg> 120 </bean> 121 </entry> 122 </map> 123 </property> 124 </bean> 125 </beans>
6.在java目录下创建com.cong.ui.Test类
1 package com.cong.ui; 2 3 4 import com.cong.pojo.Car; 5 import com.cong.pojo.User; 6 import com.cong.pojo.Week; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 10 public class Test { 11 public static void main(String[] args) { 12 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); 13 Car benchi = (Car) context.getBean("benchi"); 14 Car baoma = (Car) context.getBean("baoma"); 15 System.out.println(benchi.toString()); 16 System.out.println(baoma.toString()); 17 Week week = (Week) context.getBean("week"); 18 System.out.println(week.toString()); 19 User cong = (User) context.getBean("cong"); 20 System.out.println(cong.toString()); 21 User rainbow = (User) context.getBean("rainbow"); 22 System.out.println(rainbow.toString()); 23 User will = (User) context.getBean("Will"); 24 System.out.println(will.toString()); 25 } 26 }
7.运行结果
8.完整目录