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 ...
随机推荐
- 【Linux】Linux系统启动过程
1.Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立终端 . 用户登录系统. 1.Linux系统的启动过程并不是大家想象 ...
- springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同. 主要讲maven的pom.xml和一些配置变化,详细说明. 软件简介 Spring是一个流行 ...
- Windows未能启动 由于关键系统驱动程序丢失或损坏 电脑无法开机
该错误导致系统无法开机,其实也好解决 错误描述: Windows未能启动.原因可能是最近更改了硬盘或软件.解决此问题的步骤…… 1.…… 2.…… 3.…… …… 文件:\windows\system ...
- UDP client,UDP server, TCP server, TCP client
UDP server import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocke ...
- vuex深入浅出
本文主要记录使用vuex的使用场景.重要组成部分和学习心得. 1.说在前面 学习vue有两周的时间了,目前已经对vue的基础使用比较熟悉了.但是一直对vuex的使用耿耿于怀,这么说是因为总是不太理解, ...
- The twentyth day
10th Dec 2018 Cause It's hard for me to lose in my life I've found 因为失去你是一种煎熬 Only time will tell a ...
- MongoDB学习之mongoose
MongoDB介绍: MongoDB是基于Javascript语言的数据库,存储格式是JSON,而Node也是基于JavaScript的环境(库),所以node和mongoDB的搭配能减少因为数据转换 ...
- 【阿里云产品公测】小白对OTS两点小建议
作者:阿里云用户荷包蛋 我是大一的新生,作为一个爱技术爱学习爱折腾的熊孩子,我在暑假申请了ECS,学到了很多东西.现在阿里巴巴又开放了很多免费测试,我抱着学习和围观的心态申请了测试,其中有OTS这个高 ...
- SharePoint 2013 - System Features
1. Embed Information & Convert to PDF 功能,在文档的preview界面(hover panel); 2. Share功能可以选择是否发送邮件 -- Don ...
- 大数据的正确用法你get到了吗?
Azure 镜像市场已于2016年9月21日正式上线,在这个统一的集成平台中,客户可以轻松地浏览.搜索和选择一系列来自第三方的应用和解决方案,并可以将其快速一键部署到 Azure 实例当中. 在移动为 ...