Spring IOC&DI之Setter注入

上一节总结了依赖注入中的构造器注入方式,本节总结基于Setter方式的注入及xml文件的配置方式。

还是根据所依赖对象的类型分为:

(1)基本类型:如int型、String型等。

(2)自定义类型:自定义的类型,也就是封装成了一个单独的bean。

(3)容器类型:如List、Set、Map等

(4)空类型

使用Setter方法注入必须要遵循javabean规范,类内部使用setter/getter方法来设置或者获取属性值。

而且在调用者类内必须要有默认构造函数。如果已经定义了含参构造函数,则必须显式的定义无参构造函数。

因为setter注入方式即调用类中的setter方法将其他属性注入,既然要调用setter方法则需要有对象,spring框架内部就是使用反射原理+类名+默认构造函数先创建一个对象,然后调用setter方法。

具体定义如下所示:

public class MyService{
	
	private MyDao myDao; //引用其他bean
	private Description des; //引用其它bean
	
	private List<String> list = new ArrayList<String>(); //list容器 
	private Set<String> set = new HashSet<String>(); //set容器
          //map容器
	private Map<String, Integer> maps = new HashMap<String, Integer>();

         //setter & getter方法
	public void setMyDao(MyDao myDao){
		this.myDao = myDao;
	}
	
	public MyDao getMyDao(){
		return myDao;
	}

	public void setDes(Description des){
		this.des = des;
	}
	
	public Description getDes(){
		return des;
	}
	
	public void setList(List<String> list){
		this.list = list;
	}
	
	public List<String> getList(){
		return list;
	}
	
	public void setSet(Set<String> set){
		this.set = set;
	}
	
	public Set<String> getSet(){
		return set;
	}
	
	public void setMaps(Map<String, Integer> maps){
		this.maps = maps;
	}
	
	public Map<String, Integer> getMaps(){
		return maps;
	}
	
	public void addUser(){
		myDao.addUser();
		des.descript();
	}
	
	public void print(){
		//打印list中的内容
		for(String str : list){
			System.out.println(str);
		}
		
		for(String str2 : set){
			System.out.println(str2);
		}
		Iterator<Map.Entry<String, Integer>> it = maps.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry<String, Integer> entry = it.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
	}
}

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:context="http://www.springframework.org/schema/context"
		
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
			default-lazy-init="true">
			<!--对所有bean进行延迟初始化-->
			<!--
			<context:component-scan base-package="com.springframework.ioc"/>
			-->
			<!-- beans declare go here -->
			<bean id="mysql" class="com.springframework.ioc.MysqlDaoImpl">
			<!--调换构造函数顺序-->
				<constructor-arg  index="1" value="1"/>
				<constructor-arg  index="0" value="mysql"/>
			</bean>
			<bean id="oracle" class="com.springframework.ioc.OracleDaoImpl">
				<property name="name" value="oracle"/>
			</bean>
			
			<bean id="description" class="com.springframework.ioc.Description"/>
			<!-- 通过setter方法注入-->
			
			<bean id = "myservice" class="com.springframework.ioc.MyService">
				<property name="myDao" ref = "mysql"/>
				<property name="des" ref = "description"/>
				<property name="list">
					<list>
						<value>xiaoming</value>
						<value>xiaohong</value>
						<value>xiaolin</value>
					</list>
				</property>
				
				<property name="set">
					<set>
						<value>jin</value>
						<value>yin</value>
						<value>tong</value>
					</set>
				</property>
				
				<property name="maps">
					<map>
						<entry key="wang" value="1"/>
						<entry key="li" value="2"/>
						<entry key="hu" value="3"/>
					</map>
				</property>
			</bean>
			
	</beans>

 总的来说就是使用

<property name=" xxx" ref = "xxx "/>

 或者是

<property name="xxx" value = "xxx"/>

猜你喜欢

转载自wgfhit.iteye.com/blog/2263089