6 DI 依赖注入

6.1 构造器注入

前面已经讲过

6.2 Set方式注入【重点】

  • 依赖注入:Set注入!
    依赖:bean对象的创建依赖于容器!
    注入:bean 对象中的所以属性,有容器来注入!
    【环境搭建】
  1. 复杂类型
package com.hunter.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  1. 真实测试对象
package com.hunter.pojo;

import java.util.*;

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set games;
    private Properties info;
    private String wife;
    ... Get Set ToString
}

  1. beans



    
    
        
    

    
        
        
        
        
        
        
            
                red building dream
                west gogogo
                water tiger roll
                three kingdoms
            
        
        
        
            
                 watch movie
                 listen music
            
        
        
        
            
                 
                 
            
        
        
        
             
                 lol
                 coc
                 bob
             
        
        
        
            
        
        
        
            
                201110437
                male
                http://www.baidu.com
                root
                123456
            
        
    


  1. 测试类
import com.hunter.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.toString());
        /*
        Student{
            name='hunter',
            address=Address{address='xian'},
            books=[
                red building dream,
                west gogogo,
                water tiger roll,
                three kingdoms],
            hobbys=[
                watch movie,
                listen music],
            card={
                IDCard=658745545645,
                BankCard=2545861235
                },
            games=[lol, coc, bob],
            info={
                un=root,
                Gender=male,
                pwd=123456,
                Num=201110437,
                Url=http://www.baidu.com
                },
            wife='null'}
        * */
    }
}

6.3 其他方式

我们可以使用p命名空间和c命名空间
官方解释


截图
  • 使用



    
    
    
    
    

  • 测试
public class MyTest {
    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user.toString());
    }
}

注意点:p名面和c命名不能直接使用,需要导入约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

6.4 bean 的作用域

截图
  • singleton 单例全局共享一个,为bean scope的默认值

  • 原型模式(prototype) 每次从容器中get的时候都会产生一个新对象

  • 其余的request、session、application只能在web开发中使用!

你可能感兴趣的:(6 DI 依赖注入)