Spring依赖注入(dependency injection)

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


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


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


Spring的注入有两类:


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




2、setter注入:


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


1)常量注入:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
	</bean>

</beans>


2)bean的注入:



给Address配置值之后如图:




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代码增加了输入注入,如图:




beans.xml代码:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
	</bean>

</beans>


测试代码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() ;
	}
	
}


运行后控制台打印信息:




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<String> hobbies ;
	
	public void setHobbies(List<String> 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中配置如下:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
		<!-- List注入 -->
		<property name="hobbies">
			<list>
				<value>羽毛球</value>
				<value>乒乓球</value>
				<value>足球</value>
				<value>台球</value>
			</list>
		</property>
		
	</bean>

</beans>

运行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() ;
	}
	
}


控制台打印如下:




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<String> hobbies ;
	private Map<String , String> cards ;
	
	public void setCards(Map<String, String> cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List<String> 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中的配置文件如下:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
		<!-- List注入 -->
		<property name="hobbies">
			<list>
				<value>羽毛球</value>
				<value>乒乓球</value>
				<value>足球</value>
				<value>台球</value>
			</list>
		</property>
		
		<!-- map注入 -->
		<property name="cards">
			<map>
				<entry key="中国银行" value="11010102020202002" />
				<entry>
					<key><value>建设银行</value></key>
					<value>20200300039399292992</value>
				</entry>
			</map>
		</property>
	</bean>

</beans>


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





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<String> hobbies ;
	private Map<String , String> cards ;
	private Set<String> games ;
	
	public void setGames(Set<String> games) {
		this.games = games;
	}
	
	public void setCards(Map<String, String> cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List<String> 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代码如下:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
		<!-- List注入 -->
		<property name="hobbies">
			<list>
				<value>羽毛球</value>
				<value>乒乓球</value>
				<value>足球</value>
				<value>台球</value>
			</list>
		</property>
		
		<!-- map注入 -->
		<property name="cards">
			<map>
				<entry key="中国银行" value="11010102020202002" />
				<entry>
					<key><value>建设银行</value></key>
					<value>20200300039399292992</value>
				</entry>
			</map>
		</property>
		
		<!-- Set集合注入 -->
		<property name="games">
			<set>
				<value>lol</value>
				<value>dota</value>
				<value>cs</value>
				<value>cf</value>
			</set>
		</property>
		
	</bean>

</beans>


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




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<String> hobbies ;
	private Map<String , String> cards ;
	private Set<String> games ;
	private String wife ;
	
	public void setWife(String wife) {
		this.wife = wife;
	}
	
	public void setGames(Set<String> games) {
		this.games = games;
	}
	
	public void setCards(Map<String, String> cards) {
		this.cards = cards;
	}
	
	public void setHobbies(List<String> 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代码:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
		<!-- List注入 -->
		<property name="hobbies">
			<list>
				<value>羽毛球</value>
				<value>乒乓球</value>
				<value>足球</value>
				<value>台球</value>
			</list>
		</property>
		
		<!-- map注入 -->
		<property name="cards">
			<map>
				<entry key="中国银行" value="11010102020202002" />
				<entry>
					<key><value>建设银行</value></key>
					<value>20200300039399292992</value>
				</entry>
			</map>
		</property>
		
		<!-- Set集合注入 -->
		<property name="games">
			<set>
				<value>lol</value>
				<value>dota</value>
				<value>cs</value>
				<value>cf</value>
			</set>
		</property>
		
		<!-- Null 注入 -->
		<property name="wife">
			<null></null>
		</property>
		
	</bean>

</beans>


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




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<String> hobbies;
	private Map<String, String> cards;
	private Set<String> 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<String> games) {
		this.games = games;
	}

	public void setCards(Map<String, String> cards) {
		this.cards = cards;
	}

	public void setHobbies(List<String> 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配置代码:


<?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就是java对象 , 由spring容器创建和管理 -->
	<bean id="addr" class="com.myspring.vo.Address">
		<property name="address" value="北京市长安街1号"></property>
	</bean>
	<bean id="student" class="com.myspring.vo.Student">
		<!-- 常量注入 -->
		<property name="name" value="张三丰"></property>
		<property name="addr" ref="addr"></property>
		<!-- 注入数组 -->
		<property name="books">
			<array>
				<value>傲慢与偏见</value>
				<value>仲夏夜之梦</value>
				<value>雾都孤儿</value>
			</array>
		</property>
		<!-- List注入 -->
		<property name="hobbies">
			<list>
				<value>羽毛球</value>
				<value>乒乓球</value>
				<value>足球</value>
				<value>台球</value>
			</list>
		</property>
		
		<!-- map注入 -->
		<property name="cards">
			<map>
				<entry key="中国银行" value="11010102020202002" />
				<entry>
					<key><value>建设银行</value></key>
					<value>20200300039399292992</value>
				</entry>
			</map>
		</property>
		
		<!-- Set集合注入 -->
		<property name="games">
			<set>
				<value>lol</value>
				<value>dota</value>
				<value>cs</value>
				<value>cf</value>
			</set>
		</property>
		
		<!-- Null 注入 -->
		<property name="wife">
			<null></null>
		</property>
		
		<!-- Properties注入 -->
		<property name="info">
			<props>
				<prop key="学号">11020039398</prop>
				<prop key="sex">男</prop>
				<prop key="name">小明</prop>
			</props>
		</property>
		
	</bean>

</beans>


运行测试类test结果如下:




9)、p命名空间注入:


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



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




下面来看一下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中的配置代码如下:


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

	<!-- bean就是java对象 , 由spring容器创建和管理 -->
	
	<!-- p命名空间注入 -->
	<bean id="user" class="com.myspring.vo.User" p:name="小明" p:age="11"></bean>

</beans>

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




测试类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);
	}
	
}


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





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代码如下:


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

	<!-- bean就是java对象 , 由spring容器创建和管理 -->
	
	<!-- c命名空间注入 -->
	<bean id="c_user" class="com.myspring.vo.User" c:name="小王" c:age="22"></bean>
	
</beans>


测试类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);
	}
	
}

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




























评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值