Spring依赖注入的三种方法

Spring 管理java bean主要以依赖注入(控制反转IOC)的方式进行管理。其中有三种方式:属性注入、构造函数、工厂方法(非静态和静态,主要是以方法是否静态进行注入)

People类:

package com.xiaowu.entity;

public class People {
	int id;
	String name;
	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 [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}

1,属性注入:

beans.xml:



	
	
  
  	
	  	
	  	
	  	
	
	
	

main方法控制台输出:

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people1 = (People) ac.getBean("people1");

		
		System.out.println(people1.toString());
	}
}

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

1)类型:

beans.xml



	
	
  
	
		
		
		
	
	
	

main方法控制台输出:

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people1 = (People) ac.getBean("people2");

		
		System.out.println(people2.toString());
	}
}

2) 通过索引

beans.xml:




	
		
		
		
	

 控制台输出:

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people3 = (People) ac.getBean("people3");

		
		System.out.println(people3.toString());
	}
}

3) 联合使用 

就是在bean标签内中的加index='0"或者type="int"

beans.xml 





	
		
		
		
	
	

main方法控制台输出:

 

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people4 = (People) ac.getBean("people4");

		
		System.out.println(people4.toString());
	}
}

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

非静态工厂:

1)首先创建工厂类:

package com.xiaowu.factory;

import com.xiaowu.entity.People;

public class PeopleFactory {
	public People crteatePeople() {
		People people = new People();
		people.setId(55);
		people.setName("xiaowu");
		people.setAge(55);
		return people;
	}
}

注意:该工厂类中的方法是非静态的 ,必须实例化工厂类(factory-bean)后才能调用工厂方法。

beans.xml:




	
	
	

在上面的xml配置文件中  先创建工厂类bean ,在创建People类的bean 加入factory-bean="peopleFactory" 和 factory-method="crteatePeople" 然后在控制台输出:

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people5 = (People) ac.getBean("people5");

		
		System.out.println(people5.toString());
	}
}

非静态工厂:

1) 创建静态工厂类

package com.xiaowu.factory;

import com.xiaowu.entity.People;

public static class PeopleFactory {
	public People crteatePeople() {
		People people = new People();
		people.setId(55);
		people.setName("xiaowu");
		people.setAge(55);
		return people;
	}
}

beans.xml:




	
	

该类中的方法是静态的 所以不需要创建该类的bean 直接使用该类的静态方法即可。

main方法控制台输出:

package com.xiaowu.test;

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

import com.xiaowu.entity.People;

public class Test2 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		

		// 属性注入
		People people6 = (People) ac.getBean("people6");

		
		System.out.println(people6.toString());
	}
}

 

 

你可能感兴趣的:(Spring)