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的更多相关文章

  1. Spring Boot 2 (三):Spring Boot 2 相关开源软件

    Spring Boot 2 (三):Spring Boot 2 相关开源软件 一.awesome-spring-boot Spring Boot 中文索引,这是一个专门收集 Spring Boot 相 ...

  2. Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire

    创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...

  3. Spring知识点总结(二)之Spring IOC

    1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入)    所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...

  4. Spring学习(三)--Spring的IOC

    1.BeanFactory和FactoryBean BeanFactory是一个接口类,定义了IOC容器最基本的形式,提供了IOC容器所应该遵守的基本服务契约. FactoryBean是一个能产生或者 ...

  5. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  6. Spring知识点总结(三)之注解方式实现IOC和DI

        1. 注解概念        所谓注解就是给程序看的提示信息,很多时候都用来作为轻量级配置的方式.        关于注解的知识点,参看java基础课程中java基础加强部分的内容.    2 ...

  7. Spring知识点小结(三)

    一.aop的简介 aop:面向切面编程    aop是一种思想,面向切面编程思想,Spring内部提供了组件对aop进行实现    aop是在运行期间使用动态代理技术实现的思想    aop是oop延 ...

  8. (转)Spring Boot 2 (三):Spring Boot 开源软件都有哪些?

    http://www.ityouknow.com/springboot/2018/03/05/spring-boot-open-source.html 2016年 Spring Boot 还没有被广泛 ...

  9. Spring Boot 2 (三):Spring Boot 开源软件都有哪些?

    016年 Spring Boot 还没有被广泛使用,在网上查找相关开源软件的时候没有发现几个,到了现在经过2年的发展,很多互联网公司已经将 Spring Boot 搬上了生产,而使用 Spring B ...

随机推荐

  1. 跨页面传值2之cookie多值使用

    单值cookie结构 CookieKeyName——CookieValue CookieKeyName2——CookieValue2 ............... 通过CookieKeyName进行 ...

  2. SpringSecurity 3.2入门(1)框架介绍

    关于Spring Security Spring Security,这是一种基于Spring AOP和Servlet过滤器 [7] 的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法 ...

  3. vs2013项目停止调试后 iis express也跟着退出

    解决方法:项目—>XX属性—>Web—>调试器—>取消[启用编辑并继续]

  4. 工作流-----WorkFlow

    我们都知道对于一个OA系统来说,最重要的也是不可或缺的一个重要环节那就是对于工作流的实现,为此,最近专门在学习如何使用WorkFlow,问前辈,前辈也说道K2工作流引擎挺不错,自己同时也翻阅了一些资料 ...

  5. 正则表达式把所有Paul替换成Ringo:Paul Puala Pualine paul Paul

    代码实现如下: <!DOCTYPE html><html><body> <h2>JavaScript Regular Expressions</h ...

  6. SQL:Example Uses of the SUBSTRING String Function

    ---Example Uses of the SUBSTRING String Function --http://www.sql-server-helper.com/tips/tip-of-the- ...

  7. Csharp: TreeView 初始化设置默认选择节点

    /// <summary> /// 设置查找的节点为选定节点 /// 涂聚文 /// 2013-07-15 /// </summary> /// <param name= ...

  8. lxml模块(应用xpath技术)

    一.lxml介绍 第三方库lxml是第一款表现出高性能特征的python xml库,天生支持Xpath1.0.XSLT1.0.定制元素类,甚至python风格的数据绑定接口.lxml是通过Cpytho ...

  9. Django的MTV模式详解

    参考博客:https://www.cnblogs.com/yuanchenqi/articles/7629939.html 一.MVC模型 Web服务器开发领域里著名的MVC模式. 所谓MVC就是把W ...

  10. 洛谷P1970 花匠(dp)

    题意 题目链接 Sol 直接用\(f[i][0/1]\)表示到第\(i\)个位置,该位置是以上升结尾还是以下降结尾 转移的时候只需枚举前一个即可 #include<cstdio> #inc ...