Spring依赖注入(dependency injection)

Spring的依赖注入包括两方面:


1、依赖:指bean对象创建依赖于容器;Bean对象的依赖资源。


2、注入:指bean对象依赖的资源由容器来设置和装配。


Spring的注入有两类:


1、构造器的注入:使用的是constructor-arg,如图:


Spring依赖注入(dependency injection)_第1张图片


2、setter注入:


要求被注入的属性,必须有set方法,set方法生成的方法名,由set+属性(首字母大写),如果属性是boolean类型,没有get方法,是is方法。


1)常量注入:





	
	
		
		
	



2)bean的注入:


Spring依赖注入(dependency injection)_第2张图片

给Address配置值之后如图:


Spring依赖注入(dependency injection)_第3张图片


Address类代码:


package com.myspring.vo;

public class Address {

	private String address ;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	
}


Student类代码:


package com.myspring.vo;

public class Student {

	private String name;
	private Address addr ;

	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
	}

}


3)数组注入:


在Student类中加入字符串数组,代码如:


package com.myspring.vo;

public class Student {

	private String name;
	private Address addr ;
	private String[] books ;
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i]+"  ");
		}
		System.out.println();
	}

}


beans.xml代码增加了输入注入,如图:


Spring依赖注入(dependency injection)_第4张图片


beans.xml代码:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
	



测试代码Test:


package com.myspring.test;

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

import com.myspring.vo.Student;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ;
		Student student = (Student) ac.getBean("student") ;
		student.show() ;
	}
	
}


运行后控制台打印信息:


Spring依赖注入(dependency injection)_第5张图片


4)List注入:


在Student实体类中增加集合属性hobbies,代码如:


package com.myspring.vo;

import java.util.List;

public class Student {

	private String name;
	private Address addr ;
	private String[] books ;
	private List hobbies ;
	
	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i]+"  ");
		}
		System.out.println();
		System.out.println("hobbies = "+ hobbies);
	}

}


在beans.xml中配置如下:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
		
		
			
				羽毛球
				乒乓球
				足球
				台球
			
		
		
	


运行test测试类代码:


package com.myspring.test;

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

import com.myspring.vo.Student;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ;
		Student student = (Student) ac.getBean("student") ;
		student.show() ;
	}
	
}


控制台打印如下:


Spring依赖注入(dependency injection)_第6张图片


5)map注入:


实体类Student中增加map属性cards,代码如:


package com.myspring.vo;

import java.util.List;
import java.util.Map;

public class Student {

	private String name;
	private Address addr ;
	private String[] books ;
	private List hobbies ;
	private Map cards ;
	
	public void setCards(Map cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i]+"  ");
		}
		System.out.println();
		System.out.println("hobbies = "+ hobbies);
		System.out.println("cards = "+ cards);
	}

}

beans.xml中的配置文件如下:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
		
		
			
				羽毛球
				乒乓球
				足球
				台球
			
		
		
		
		
			
				
				
					建设银行
					20200300039399292992
				
			
		
	



运行test类,代码同上面的test,控制台打印信息如下:


Spring依赖注入(dependency injection)_第7张图片



6)Set集合注入:


实体类Student代码如下:


package com.myspring.vo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {

	private String name;
	private Address addr ;
	private String[] books ;
	private List hobbies ;
	private Map cards ;
	private Set games ;
	
	public void setGames(Set games) {
		this.games = games;
	}
	
	public void setCards(Map cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i]+"  ");
		}
		System.out.println();
		System.out.println("hobbies = "+ hobbies);
		System.out.println("cards = "+ cards);
		System.out.println("games = " + games);
	}

}


beans.xml代码如下:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
		
		
			
				羽毛球
				乒乓球
				足球
				台球
			
		
		
		
		
			
				
				
					建设银行
					20200300039399292992
				
			
		
		
		
		
			
				lol
				dota
				cs
				cf
			
		
		
	



测试类代码test同上面,运行后控制台打印信息:


Spring依赖注入(dependency injection)_第8张图片


7)Null注入:


实体类Student代码:


package com.myspring.vo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {

	private String name;
	private Address addr ;
	private String[] books ;
	private List hobbies ;
	private Map cards ;
	private Set games ;
	private String wife ;
	
	public void setWife(String wife) {
		this.wife = wife;
	}
	
	public void setGames(Set games) {
		this.games = games;
	}
	
	public void setCards(Map cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}
	
	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name +"---addr="+addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i]+"  ");
		}
		System.out.println();
		System.out.println("hobbies = "+ hobbies);
		System.out.println("cards = "+ cards);
		System.out.println("games = " + games);
		System.out.println("wife = "+ wife );
	}

}

beans.xml代码:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
		
		
			
				羽毛球
				乒乓球
				足球
				台球
			
		
		
		
		
			
				
				
					建设银行
					20200300039399292992
				
			
		
		
		
		
			
				lol
				dota
				cs
				cf
			
		
		
		
		
			
		
		
	



运行test测试类,控制台打印信息如下:


Spring依赖注入(dependency injection)_第9张图片


8)Properties注入


实体类Student代码:


package com.myspring.vo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Student {

	private String name;
	private Address addr;
	private String[] books;
	private List hobbies;
	private Map cards;
	private Set games;
	private String wife;
	private Properties info;

	public void setInfo(Properties info) {
		this.info = info;
	}

	public void setWife(String wife) {
		this.wife = wife;
	}

	public void setGames(Set games) {
		this.games = games;
	}

	public void setCards(Map cards) {
		this.cards = cards;
	}

	public void setHobbies(List hobbies) {
		this.hobbies = hobbies;
	}

	public void setAddr(Address addr) {
		this.addr = addr;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setBooks(String[] books) {
		this.books = books;
	}

	public void show() {
		System.out.println("name=" + name + "---addr=" + addr.getAddress());
		System.out.print("books=");
		for (int i = 0; i < books.length; i++) {
			System.out.print(books[i] + "  ");
		}
		System.out.println();
		System.out.println("hobbies = " + hobbies);
		System.out.println("cards = " + cards);
		System.out.println("games = " + games);
		System.out.println("wife = " + wife);
		System.out.println("info = " + info);
	}

}

beans.xml配置代码:





	
	
		
	
	
		
		
		
		
		
			
				傲慢与偏见
				仲夏夜之梦
				雾都孤儿
			
		
		
		
			
				羽毛球
				乒乓球
				足球
				台球
			
		
		
		
		
			
				
				
					建设银行
					20200300039399292992
				
			
		
		
		
		
			
				lol
				dota
				cs
				cf
			
		
		
		
		
			
		
		
		
		
			
				11020039398
				
				小明
			
		
		
	



运行测试类test结果如下:


Spring依赖注入(dependency injection)_第10张图片


9)、p命名空间注入:


我们从下面的文档截图中可以看到这是p命名空间的头文件,该文档所在路径可以从图中看到,


Spring依赖注入(dependency injection)_第11张图片


p命名空间的头文件部分和普通的头文件部分唯一的不同就是p命名空间的头文件多了xmlns:p="" 这部分,如图:


Spring依赖注入(dependency injection)_第12张图片


下面来看一下p命名怎么注入:

新建一个实体类User,代码如下:


package com.myspring.vo;

public class User {

	private String name;
	private int age;

	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 "User [name=" + name + ", age=" + age + "]";
	}

}

beans.xml中的配置代码如下:





	
	
	
	


其中name和age是对应的实体类User中的name和age属性


Spring依赖注入(dependency injection)_第13张图片


测试类test代码如下:


package com.myspring.test;

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

import com.myspring.vo.Student;
import com.myspring.vo.User;

public class Test {

	public static void main(String[] args) {
		
		//p命名注入测试
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ;
		User user = (User) ac.getBean("user") ;
		System.out.println(user);
	}
	
}


控制台打印的运行结果如下:


Spring依赖注入(dependency injection)_第14张图片



3、c命名空间注入(其实就是构造方法注入)


c命名空间注入需要现在beans.xml头文件部分增加

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


实体类User代码如下:


package com.myspring.vo;

public class User {

	private String name;
	private int age;

	public User() {
		// TODO Auto-generated constructor stub
	}

	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	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 "User [name=" + name + ", age=" + age + "]";
	}

}


beans.xml代码如下:





	
	
	
	
	


测试类Test代码:


package com.myspring.test;

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

import com.myspring.vo.Student;
import com.myspring.vo.User;

public class Test {

	public static void main(String[] args) {
		
		//p命名注入测试
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ;
		User user = (User) ac.getBean("c_user") ;
		System.out.println(user);
	}
	
}

控制台打印运行结果如下:


Spring依赖注入(dependency injection)_第15张图片


























你可能感兴趣的:(spring)