首页 > 代码库 > 《一头扎进Spring4》学习笔记(二)Spring ioc 之详解

《一头扎进Spring4》学习笔记(二)Spring ioc 之详解

第一节 Spring ioc 简介

IOC(控制反转:Inverse of Control),又称作 依赖注入,是一种重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。

第二节 Spring ico 实例

1、接口类

1 public interface Tester {
2   public void test();
3 }

2、实体类继承接口类

 1 public class Lisi implements Tester{
 2     public void test(){
 3         System.out.println("李四-测试程序");
 4     }
 5 }
 6 
 7 public class ZhangSan implements Tester{
 8     public void test(){
 9         System.out.println("张三-测试程序");
10     }
11 }        

3、控制类

public class JavaWork {
    
    private Tester tester;
    
    public void setTester(Tester tester) {
        this.tester = tester;
    }

    public void doTest(){        
        tester.test();
    }
}

4、beans.xml,Spring管理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.xsd">
 6 
 7     <bean id="zhangsan" class="com.java1234.service.ZhangSan"></bean>
 8     
 9     <bean id="lisi" class="com.java1234.service.Lisi"></bean>
10     
11     <bean id="javaWork" class="com.java1234.service.JavaWork">
12         <property name="tester" ref="lisi"></property>
13     </bean>
14   
15 </beans>

property标签设置参数,ref 设置会自动调用JavaWork的set方法给tester赋值

5、测试类

1 public class Test2 {
2   
3     public static void main(String[] args) {
4         ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
5         JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
6         javaWork.doTest();
7     }
8 }

第三节 装配一个bean 

第四节 依赖注入

1、属性注入

2、构造函数注入(通过类型,通过索引,联合使用)

3、工厂方法注入(非静态工厂,静态工厂)

4、泛型依赖注入

5、实例:

 技术分享

实体类People

 1 package com.java1234.entity;
 2 
 3 public class People {
 4 
 5     private int id;
 6     private String name;
 7     private int age;
 8     
 9     public People() {
10         super();
11         // TODO Auto-generated constructor stub
12     }
13         
14     public People(int id, String name, int age) {
15         super();
16         this.id = id;
17         this.name = name;
18         this.age = age;
19     }
20 
21     public int getId() {
22         return id;
23     }
24     public void setId(int id) {
25         this.id = id;
26     }
27     public String getName() {
28         return name;
29     }
30     public void setName(String name) {
31         this.name = name;
32     }
33     public int getAge() {
34         return age;
35     }
36     public void setAge(int age) {
37         this.age = age;
38     }
39 
40     @Override
41     public String toString() {
42         return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
43     }
44     
45 }

非静态工厂类

 1 package com.java1234.factory;
 2 import com.java1234.entity.People;
 3 
 4 public class PeopleFactory {
 5 
 6     public People createPeople(){
 7         People p=new People();
 8         p.setId(5);
 9         p.setName("小七");
10         p.setAge(77);
11         return p;
12     }
13 }

静态工厂类

 1 package com.java1234.factory;
 2 import com.java1234.entity.People;
 3 
 4 public class PeopleFactory2 {
 5 
 6     public static People createPeople(){
 7         People p=new People();
 8         p.setId(8);
 9         p.setName("小八");
10         p.setAge(88);
11         return p;
12     }
13 }

beans.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 
 7     <bean id="people" class="com.java1234.entity.People"></bean>
 8     
 9     <bean id="people2" class="com.java1234.entity.People">
10         <property name="id" value="1"></property>
11         <property name="name" value="张三"></property>
12         <property name="age" value="11"></property>
13     </bean>
14     
15     <bean id="people3" class="com.java1234.entity.People">
16         <constructor-arg type="int" value="2"></constructor-arg>
17         <constructor-arg type="String" value="李四"></constructor-arg>
18         <constructor-arg type="int" value="22"></constructor-arg>
19     </bean>
20     
21     <bean id="people4" class="com.java1234.entity.People">
22         <constructor-arg index="0" value="3"></constructor-arg>
23         <constructor-arg index="1" value="王五"></constructor-arg>
24         <constructor-arg index="2" value="55"></constructor-arg>
25     </bean>
26     
27     <bean id="people5" class="com.java1234.entity.People">
28         <constructor-arg index="0" type="int" value="4"></constructor-arg>
29         <constructor-arg index="1" type="String" value="招六"></constructor-arg>
30         <constructor-arg index="2" type="int" value="66"></constructor-arg>
31     </bean>
32     
33     <bean id="peopleFactory" class="com.java1234.factory.PeopleFactory"></bean>
34     
35     <bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
36   
37       <bean id="people8" class="com.java1234.factory.PeopleFactory2" factory-method="createPeople"></bean>
38 </beans>

  注:

    1) 属性注入 property[name,value]

    2) 构造函数注入 constructor-arg[type|index,value]

    3) 工厂注入 非静态工厂先声明工厂bean,再通过 factory-bean、factory-method 引用

          静态工厂直接指定工厂类class,再 factory-method 

测试类

 1 package com.java1234.test;
 2 import org.springframework.context.ApplicationContext;
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 import com.java1234.entity.People;
 5 
 6 public class Test2 {
 7 
 8     public static void main(String[] args) {
 9         ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
10         People people=(People)ac.getBean("people");
11         System.out.println(people);
12         
13         // 属性注入
14         People people2=(People)ac.getBean("people2");
15         System.out.println(people2);
16         
17         // 构造方法注入
18         People people3=(People)ac.getBean("people3");
19         System.out.println(people3);
20         
21         People people4=(People)ac.getBean("people4");
22         System.out.println(people4);
23         
24         People people5=(People)ac.getBean("people5");
25         System.out.println(people5);
26         
27         // 工厂方法注入
28         People people7=(People)ac.getBean("people7");
29         System.out.println(people7);
30         
31         People people8=(People)ac.getBean("people8");
32         System.out.println(people8);
33     }
34 }

 

 

 

 

 第五节 注入参数

 

《一头扎进Spring4》学习笔记(二)Spring ioc 之详解