第一节:spring ioc 简介

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

 

第二节:spring ioc 实例讲解

 

package com.wishwzp.service;

public interface Tester {
    
    public void test();

}
package com.wishwzp.service;

public class LiSi implements Tester{

    public void test(){
        System.out.println("李四-测试程序");
    }
}
package com.wishwzp.service;

public class ZhangSan implements Tester{

    public void test(){
        System.out.println("张三-测试程序");
    }
}
package com.wishwzp.service;

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

    public void doTest(){
//        ZhangSan zhangsan = new ZhangSan();
//        zhangsan.test();
        
        tester.test();
    }

}
package com.wishwzp.test;

import com.wishwzp.service.JavaWork;
import com.wishwzp.service.LiSi;

public class Test {

    /**
     * 主管执行命令
     * @param args
     */
    public static void main(String[] args) {
        JavaWork javawork = new JavaWork();
        javawork.setTester(new LiSi());
        javawork.doTest();
    }
}

 

(二)Spring 之IOC 详解

 

package com.wishwzp.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wishwzp.service.JavaWork;
import com.wishwzp.service.Tester;

public class Test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        
//        Tester t = null;
//        t=(Tester) ac.getBean("zhangsan");
//        t.test();
        
        JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
        javaWork.doTest();
        
    }
}

(二)Spring 之IOC 详解

 

 

第三节:装配一个bean

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="zhangsan" class="com.wishwzp.service.ZhangSan"></bean>
    
    <bean id="lisi" class="com.wishwzp.service.LiSi"></bean>
    
    <bean id="javaWork" class="com.wishwzp.service.JavaWork">
        <property name="tester" ref="zhangsan"></property>
    </bean>
  
</beans>

 

 


 

 

第四节:依赖注入

1,属性注入;

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

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

4,泛型依赖注入;(Spring4 整合Hibernate4 的时候顺带说)

 

 

package com.wishwzp.entity;

public class People {

    private int id;
    private String name;
    private int age;
    
    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People [>;
    }
    
}

 

package com.wishwzp.factory;

import com.wishwzp.entity.People;

public class PeopleFactory {

    public People createPeople(){
        People p=new People();
        p.setId(5);
        p.setName("小七");
        p.setAge(77);
        return p;
    }
}
package com.wishwzp.factory;

import com.wishwzp.entity.People;

public class PeopleFactory2 {

    public static People createPeople(){
        People p=new People();
        p.setId(8);
        p.setName("小八");
        p.setAge(88);
        return p;
    }
    
}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="people" class="com.wishwzp.entity.People"></bean>
    
  <!-- 属性注入 --> <bean id="people2" class="com.wishwzp.entity.People"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="11"></property> </bean>
  <!-- 构造方法注入----通过类型--> <bean id="people3" class="com.wishwzp.entity.People"> <constructor-arg type="int" value="2"></constructor-arg> <constructor-arg type="String" value="李四"></constructor-arg> <constructor-arg type="int" value="22"></constructor-arg> </bean>
  <!-- 构造方法注入---通过索引--> <bean id="people4" class="com.wishwzp.entity.People"> <constructor-arg index="0" value="3"></constructor-arg> <constructor-arg index="1" value="王五"></constructor-arg> <constructor-arg index="2" value="55"></constructor-arg> </bean>
  <!-- 构造方法注入---联合使用--> <bean id="people5" class="com.wishwzp.entity.People"> <constructor-arg index="0" type="int" value="4"></constructor-arg> <constructor-arg index="1" type="String" value="招六"></constructor-arg> <constructor-arg index="2" type="int" value="66"></constructor-arg> </bean>
  <!-- 工厂方法注入 --> <bean id="peopleFactory" class="com.wishwzp.factory.PeopleFactory"></bean>
  <!-- 非静态工厂 --> <bean id="people7" factory-bean="peopleFactory" factory-method="createPeople"></bean>
  <!-- 静态工厂 --> <bean id="people8" class="com.wishwzp.factory.PeopleFactory2" factory-method="createPeople"></bean> </beans>
package com.wishwzp.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wishwzp.entity.People;


public class Test2 {

    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
        People people=(People)ac.getBean("people");
        System.out.println(people);
        
        // 属性注入
        People people2=(People)ac.getBean("people2");
        System.out.println(people2);
        
        // 构造方法注入
        People people3=(People)ac.getBean("people3");
        System.out.println(people3);
        
        People people4=(People)ac.getBean("people4");
        System.out.println(people4);
        
        People people5=(People)ac.getBean("people5");
        System.out.println(people5);
        
        // 工厂方法注入
        People people7=(People)ac.getBean("people7");
        System.out.println(people7);
        
        People people8=(People)ac.getBean("people8");
        System.out.println(people8);
    }
}

(二)Spring 之IOC 详解


 

 

 

 

第五节:注入参数

1,基本类型值;

2,注入bean;

3,内部bean;

4,null 值;

5,级联属性;

6,集合类型属性;

 

1,基本类型值;

 1 package com.wishwzp.test;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import com.wishwzp.entity.People;
 9 
10 public class T {
11 
12     private ApplicationContext ac;
13 
14     @Before
15     public void setUp() throws Exception {
16         ac=new ClassPathXmlApplicationContext("beans.xml");
17     }
18 
19     // 基本类型值
20     @Test
21     public void test1() {
22         People people=(People)ac.getBean("people1");
23         System.out.println(people);
24     }
25     
26     
27 }
T.java

相关文章:

  • 2021-07-20
  • 2021-06-30
  • 2022-12-23
  • 2022-01-17
  • 2021-12-17
猜你喜欢
  • 2021-06-05
  • 2022-01-07
  • 2021-07-21
  • 2021-08-24
  • 2021-12-03
  • 2021-11-27
相关资源
相似解决方案