Spring知识点总结(三)之Spring DI
1. IOC(DI) - 控制反转(依赖注入)
所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生命周期的管理,而是在需要时由Spring框架提供,这个由spring框架管理对象创建和生命周期的机制称之为控制反转。而在 创建对象的过程中Spring可以依据配置对对象的属性进行设置,这个过称之为依赖注入,也即DI。
- 2. set方法注入
通常的javabean属性都会私有化,而对外暴露setXxx()getXxx()方法,此时spring可以通过这样的setXxx()方法将属性的值注入对象。
a. Spring内置的可直接注入类型的注入:
1 package cn.tedu.beans;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Properties;
6 import java.util.Set;
7
8 public class Hero {
9 private int id;
10 private String name;
11 private List<String> jobs;
12 private Set<String> set;
13 private Map<String,String> map;
14 private Properties prop;
15
16 public void setId(int id) {
17 this.id = id;
18 }
19
20 public void setName(String name) {
21 this.name = name;
22 }
23
24 public void setJobs(List<String> jobs) {
25 this.jobs = jobs;
26 }
27
28 public void setSet(Set<String> set) {
29 this.set = set;
30 }
31
32 public void setMap(Map<String, String> map) {
33 this.map = map;
34 }
35
36 public void setProp(Properties prop) {
37 this.prop = prop;
38 }
39
40 @Override
41 public String toString() {
42 return "Hero [id=" + id + ", name=" + name + ", jobs=" + jobs
43 + ", set=" + set + ", map=" + map + ", prop=" + prop + "]";
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
5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
6 >
7
8 <bean id="hero" class="cn.tedu.beans.Hero">
9 <property name="id" value="123"></property>
10 <property name="name" value="亚瑟 "></property>
11 <property name="jobs">
12 <list>
13 <value>上单</value>
14 <value>打野</value>
15 <value>辅助</value>
16 <value>中单</value>
17 </list>
18 </property>
19 <property name="set">
20 <set>
21 <value>aaa</value>
22 <value>bbb</value>
23 <value>ccc</value>
24 <value>aaa</value>
25 </set>
26 </property>
27 <property name="map">
28 <map>
29 <entry key="addr" value="王者荣耀"></entry>
30 <entry key="addr" value="英雄联盟"></entry>
31 <entry key="skill" value="风火轮"></entry>
32 <entry key="age" value="19"></entry>
33 </map>
34 </property>
35 <property name="prop">
36 <props>
37 <prop key="k1">v1</prop>
38 <prop key="k2">v2</prop>
39 <prop key="k3">v3</prop>
40 <prop key="k4">v4</prop>
41 </props>
42 </property>
43 </bean>
44
45 </beans>
测试
1 @Test
2 /**
3 * SpringDI set方式属性注入 - Spring内置的可直接注入类型的注入
4 */
5 public void test1(){
6 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7 Hero hero = (Hero) context.getBean("hero");
8 System.out.println(hero);
9 }
- b. 非Spring内置的可以直接注入类型的注入:
1 package cn.tedu.beans;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Properties;
6 import java.util.Set;
7
8 public class Hero {
9 private int id;
10 private String name;
11 private List<String> jobs;
12 private Set<String> set;
13 private Map<String,String> map;
14 private Properties prop;
15 private Dog dog;
16 private Cat cat;
17
18
19 public void setId(int id) {
20 this.id = id;
21 }
22
23 public void setName(String name) {
24 this.name = name;
25 }
26
27 public void setJobs(List<String> jobs) {
28 this.jobs = jobs;
29 }
30
31 public void setSet(Set<String> set) {
32 this.set = set;
33 }
34
35 public void setMap(Map<String, String> map) {
36 this.map = map;
37 }
38
39 public void setProp(Properties prop) {
40 this.prop = prop;
41 }
42
43 public void setDog(Dog dog) {
44 this.dog = dog;
45 }
46
47 public void setCat(Cat cat) {
48 this.cat = cat;
49 }
50
51 @Override
52 public String toString() {
53 return "Hero [id=" + id + ", name=" + name + ", jobs=" + jobs
54 + ", set=" + set + ", map=" + map + ", prop=" + prop + ", dog="
55 + dog + ", cat=" + cat + "]";
56 }
57
58 }
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-3.2.xsd"
6 >
7
8 <bean id="hero" class="cn.tedu.beans.Hero">
9 <property name="id" value="123"></property>
10 <property name="name" value="亚瑟 "></property>
11 <property name="jobs">
12 <list>
13 <value>上单</value>
14 <value>打野</value>
15 <value>辅助</value>
16 <value>中单</value>
17 </list>
18 </property>
19 <property name="set">
20 <set>
21 <value>aaa</value>
22 <value>bbb</value>
23 <value>ccc</value>
24 <value>aaa</value>
25 </set>
26 </property>
27 <property name="map">
28 <map>
29 <entry key="addr" value="王者荣耀"></entry>
30 <entry key="addr" value="英雄联盟"></entry>
31 <entry key="skill" value="风火轮"></entry>
32 <entry key="age" value="19"></entry>
33 </map>
34 </property>
35 <property name="prop">
36 <props>
37 <prop key="k1">v1</prop>
38 <prop key="k2">v2</prop>
39 <prop key="k3">v3</prop>
40 <prop key="k4">v4</prop>
41 </props>
42 </property>
43 <property name="dog" ref="dog"></property>
44 <property name="cat" ref="cat"></property>
45 </bean>
46
47 <bean id="dog" class="cn.tedu.beans.Dog"></bean>
48 <bean id="cat" class="cn.tedu.beans.Cat"></bean>
49
50 </beans>
测试:
1 @Test
2 /**
3 * SpringDI set方式属性注入 - 非Spring内置的可以直接注入类型的注入
4 */
5 public void test2(){
6 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7 Hero hero = (Hero) context.getBean("hero");
8 System.out.println(hero);
9 }
- 3. 基于构造方法的注入
对象属性设置的另一种方式是在对象创建的过程中通过构造方法传入并设置对象的属性。
spring也可以通过这样的构造方法实现属性的注入。
1 package cn.tedu.beans;
2
3 public class Student {
4 private int id;
5 private String name;
6 private Dog dog;
7
8 public Student(int id, String name, Dog dog) {
9 this.id = id;
10 this.name = name;
11 this.dog = dog;
12 }
13
14 @Override
15 public String toString() {
16 return "Student [id=" + id + ", name=" + name + ", dog=" + dog + "]";
17 }
18 }
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-3.2.xsd"
6 >
7
8 <bean id="student" class="cn.tedu.beans.Student">
9 <!--
10 index:为构造方法的第几个参数 进行配置
11 name:为构造方法的哪个名字的参数进行配置
12 **index 和 name 可以配置任何一个或同时配置 但要求一旦配置必须正确
13 **推荐优先使用index方式配置 防止没有源码造成name无法匹配到对应参数
14 type:该构造方法参数的类型
15 value:该构造方法参数的值 ,用来指定基本值
16 ref:该构造方法参数的值,用来指定引用其他bean的值
17 -->
18 <constructor-arg index="0" name="id" value="999"/>
19 <constructor-arg index="1" type="java.lang.String" value="张无忌"/>
20 <constructor-arg name="dog" ref="dog"/>
21 </bean>
22
23 <bean id="dog" class="cn.tedu.beans.Dog"></bean>
测试
</beans>
1 @Test
2 /**
3 * SpringDI 构造方法方式属性注入
4 */
5 public void test3(){
6 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7 Student student = (Student) context.getBean("student");
8 System.out.println(student);
9 }
- 4. 自动装配
在Spring的set方式实现的注入过程中,支持自动装配机制,所谓自动装配机制,会根据要设置的javabean属性的名字 或 类型 到spring中自动寻找对应id 或 类型的<bean>进行设置,从而 省去依次配置的过程,简化了配置。
为 指定<bean>开启自动装配:
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-3.2.xsd"
6 >
7
8 <!--
9 autowire设定自动装配:
10 byName:根据javabean中需要注入的属性的名字 ,在spring容器中找对应id的<bean>将该<bean>的对象复制给 当前的属性
11 byType:根据javabean中需要注入的属性的类型,在spring容器中找对应class类型的<bean>将该<bean>的对象复制给 当前的属性
12 **byType方式 根据类型进行匹配,可能匹配到多个<bean>,此时会抛出异常。而byName是通过id来寻找<bean>,id没有重复,不会有这方面的问题,所以推荐使用byName方式
13 -->
14 <bean id="teacher" class="cn.tedu.beans.Teacher" autowire="byName"></bean>
15 <bean id="dog" class="cn.tedu.beans.Dog"></bean>
16 <bean id="cat" class="cn.tedu.beans.Cat"></bean>
17
18 </beans>
为全局配置自动装配:
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-3.2.xsd"
6 default-autowire="byName"
7 >
8
9 <!--
10 autowire设定自动装配:
11 byName:根据javabean中需要注入的属性的名字 ,在spring容器中找对应id的<bean>将该<bean>的对象复制给 当前的属性
12 byType:根据javabean中需要注入的属性的类型,在spring容器中找对应class类型的<bean>将该<bean>的对象复制给 当前的属性
13 **byType方式 根据类型进行匹配,可能匹配到多个<bean>,此时会抛出异常。而byName是通过id来寻找<bean>,id没有重复,不会有这方面的问题,所以推荐使用byName方式
14 -->
15 <bean id="teacher" class="cn.tedu.beans.Teacher"></bean>
16 <bean id="dog" class="cn.tedu.beans.Dog"></bean>
17 <bean id="cat" class="cn.tedu.beans.Cat"></bean>
18
19 </beans>
1 package cn.tedu.beans;
2
3 public class Teacher {
4 private Dog dog;
5 private Cat cat;
6 public void setDog(Dog dog) {
7 this.dog = dog;
8 }
9 public void setCat(Cat cat) {
10 this.cat = cat;
11 }
12
13 @Override
14 public String toString() {
15 return "Teacher [dog=" + dog + ", cat=" + cat + "]";
16 }
17 }
测试
1 @Test
2 /**
3 * SpringDI 自动装配
4 */
5 public void test4(){
6 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
7 Teacher teacher = (Teacher) context.getBean("teacher");
8 System.out.println(teacher);
9 }
Spring知识点总结(三)之Spring DI的更多相关文章
- Spring Boot 2 (三):Spring Boot 2 相关开源软件
Spring Boot 2 (三):Spring Boot 2 相关开源软件 一.awesome-spring-boot Spring Boot 中文索引,这是一个专门收集 Spring Boot 相 ...
- Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire
创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...
- Spring知识点总结(二)之Spring IOC
1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...
- Spring学习(三)--Spring的IOC
1.BeanFactory和FactoryBean BeanFactory是一个接口类,定义了IOC容器最基本的形式,提供了IOC容器所应该遵守的基本服务契约. FactoryBean是一个能产生或者 ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring知识点总结(三)之注解方式实现IOC和DI
1. 注解概念 所谓注解就是给程序看的提示信息,很多时候都用来作为轻量级配置的方式. 关于注解的知识点,参看java基础课程中java基础加强部分的内容. 2 ...
- Spring知识点小结(三)
一.aop的简介 aop:面向切面编程 aop是一种思想,面向切面编程思想,Spring内部提供了组件对aop进行实现 aop是在运行期间使用动态代理技术实现的思想 aop是oop延 ...
- (转)Spring Boot 2 (三):Spring Boot 开源软件都有哪些?
http://www.ityouknow.com/springboot/2018/03/05/spring-boot-open-source.html 2016年 Spring Boot 还没有被广泛 ...
- Spring Boot 2 (三):Spring Boot 开源软件都有哪些?
016年 Spring Boot 还没有被广泛使用,在网上查找相关开源软件的时候没有发现几个,到了现在经过2年的发展,很多互联网公司已经将 Spring Boot 搬上了生产,而使用 Spring B ...
随机推荐
- Design Pattern理解碎片
开发封闭原则(Open-Closed Principle OCP)Software entities(classes,modules,functions etc) should open for ex ...
- embedded tomcat运行java web,Unable to compile class for JSP
环境 eclipse:4.5.2 jre:1.8 java project compiler:1.8 embedded tomcat:7.0.32 可以正常启动,但是访问时,会报错. HTTP Sta ...
- jquery——write less,do more
rite less, do more.这句话想必是很多语言都提倡的. 在此举三个jquery的应用体现 一.绑定多个事件类型 $("div").bind("mouseov ...
- 阻止冒泡 table表格取消选中最后一列
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【阿里云产品公测】PTS压力测试服务器性能
作者:阿里云用户xsnjxjj 在PTS服务之前,经常使用webbench来对服务器进行压力测试,在看到阿里云PTS服务的介绍以后,深深的被PTS强大的功能所吸引 非常感谢阿里云团队给予的测试 ...
- PHP:数字转Excel列头
转自我的个人博客:阔野飞花 http://www.rexcao.net/archives/169 前段时间升级一个项目的Excel导出功能,这次的列数大概有60多条,在处理过程中发现一个问题,原先做好 ...
- matlab练习程序(圆柱投影)
圆柱投影就是将一张二维的图像投影到三维的圆柱体上,不过在显示图像的时候依然是以二维的形式给出. 投影最重要的步骤就是计算投影变换公式,和图像旋转类似,只要得到变换公式,再依照公式进行代码编写就很容易了 ...
- 在科技圈不懂“机器学习”?那你就out了
当联网的终端设备越来越多时,产生的信息数据也将呈指数式增长,大型.复杂.增长快速的数据收集已经无处不在.而机器学习能够扩增这些数据的价值,并基于这些趋势提出更广泛的应用情境. 那么,被人们津津乐道的机 ...
- winform中 让 程序 自己重启
private void button1_Click(object sender, EventArgs e) { Application.ExitThread(); ...
- Linux --Mysql数据库搭建
Mysql数据库 安装 准备: [root@localhost /]# rpm -e mysql --nodeps 将rpm方式安装的mysql卸载 [root@localhost /]# gro ...