Dependency Injection三种方式

DI的三种实现方式
  1. 设置注入—Setter Injection
  2. 构造方法注入—Constructor Injection
  3. 接口注入—Interface Injection

public class Person{
    private Dog dog;
    private Ingteger age;
    private String name;
    //setter
    //getter
}

    
    
    


    

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
Person person=ctx.getBean("person",Person.class);

这种方法要求属性必须实现setter
ref=""表示对象的引用
ref用来指向其他的bean
另外注入集合
< list >:注入一列值,允许重复
< set >:注入一列值,不允许重复
< map >:注入键(名)值对的集合,名称和值可以是任何类型
< props >:注入键(名)值对的集合,名称和值可以是任何类

private List list;
private Map map;
private Properties prop;

    
        
        
            list1
            list1
        
    
    
        
        
            
                18
            
        
    
    
        
        
            
        
    

还有一种有趣的方式spring表达式,类似于EL表达式,可以读取一个bean对象或者集合中的内容:#{bean.属性}


    	
    	


public Person(Integer age,String name,Dog dog){
    this.age = age;
    //...
}

    
    
    

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 
Person person=ctx.getBean("person",Person.class);

构造方法中的参数必须与配置文件中的参数个数一直否则报错
由于java中不会维护形参的名称,所以使用name属性进行注入时可能会有风险

public Person(Integer age,String args0,Dog dog){}

Interface Injection
它是在一个接口中定义需要注入的信息,并通过接口完成注入。Apache Avalon是一个较为典型的接口注入形IOC容器
以用户注册为例,首先定义一个接口,它的用途是将一个返回一个Person实例

public interface PersonInterfaceInject {
   public Person getPerson();
}

同时,我们需要配置一个xml


	
	
	
		 
	

最后测试一下

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");	
PersonInterfaceInject personInterfaceInject=applicationContext.getBean("personInterfaceInject",Person.class);
Person person1=personInterfaceInject.getPerson();
Person Person2=personInterfaceInject.getPerson();
System.out.println(person1);
System.out.println(person2);
System.out.println("person2==person1?:"+(person2==person1));
name:胡八万 age:18
name:胡八万 age:18
person2==person1?:false

最后输出可以看到person1和person2是不同实例
理解:“Lookup方法”可以使Spring替换一个bean原有的,获取其它对象具体的方法,并自动返回在容器中的查找结果。

参考
依赖注入的三种实现形式
零基础学习spring
Spring - lookup-method方式实现依赖注入

你可能感兴趣的:(java基础,spring,DI,spring)