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. nyoj 211——Cow Contest——————【floyd传递闭包】

    Cow Contest 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...

  2. [转]Debugging into .NET Core源代码的两种方式

    本文转自:http://www.cnblogs.com/maxzhang1985/p/6015719.html 阅读目录 一.前言 二.符号服务器 三.项目中添加ASP.NET Core源代码 四.写 ...

  3. UML建模概述

    UML的组成主要有事物.图.关系. UML中的事物: (1)构件事物:UML模型的静态部分,描述概念或物理元素,包括以下 a. 类:类是对一组具有相同属性.相同操作.相同关系和相同语义的对象的抽象.包 ...

  4. jstl标注标签库

    1.  常用标签 引入标签库: <%@ taglib prefix=”c” uri=”” %> 1.      C 标签   (1)<c:out value=”” default=” ...

  5. bootstrap dialog对话框,完成操作提示框

    1. 依赖文件: bootstrap.js bootstrap-dialog.js bootstrap.css bootstrap-dialog.css 2.代码 BootstrapDialog.co ...

  6. IntelliJ IDEA 2017.2 下载和破解方法

    一.IntelliJ IDEA 2017 下载地址  http://www.jetbrains.com/idea/#chooseYourEdition 要下载付费版的,免费版的很多功能不能用 二.破解 ...

  7. 理解JavaScript作用域

    这是一篇译文,这里贴上译文地址:http://www.zcfy.cc/article/understanding-scope-in-javascript-8213-scotch-4075.html 这 ...

  8. Dozer 实现对象间属性复制

    使用场景:两个领域之间对象转换. 比如:在系统分层解耦过程中, 对外facade接口,一般使用VO对象,而内core业务逻辑层或者数据层通常使用Entity实体. VO对象 package com.m ...

  9. Java 重写hashCode() 时为什么要用 31 来计算

    在OSChina 中看到了一篇文章<Java 中正确使用 hashCode 和 equals 方法>,看到 hashCode 的方法体内的31比较有意思. 在Stackoverflow上找 ...

  10. JsonConvert序列化问题

    返回的Json数据如下: [[1400025600,9633460,9667535,2698.09,2734.73,2749,2698.08,25333.3057,11784.9,13548.4,69 ...