[Spring]依赖注入/注入集合

依赖注入的方式

set方法注入

public class daoBean implements daoInterface {
	@Override
	public void add(){
		System.out.println("daobean中的bean");
	}
package com.yiki.bean.imp;

import com.yiki.daobean.daoBean;
import com.yiki.service.Service;

/*业务bean*/
public class ServiceBean implements Service {
	private daoBean daobean;
	
	
	public daoBean getDaobean() {
		return daobean;
	}


	public void setDaobean(daoBean daobean) {
		this.daobean = daobean;
	}


	@Override
	public void save(){
		daobean.add();//直接调用被注入的对象
	}

}
<!-- 依赖注入开始 【ref】被注入的bean的名称 -->
	<!-- set -->
	<bean id="daoBean" class="com.yiki.daobean.daoBean"></bean>
	<bean id="serviceBean" class="com.yiki.bean.imp.ServiceBean">
		<property name="daobean" ref="daoBean"></property>
	</bean>

构造方法注入

package com.yiki.bean.imp;

import com.yiki.daobean.daoBean;
import com.yiki.service.Service;

/*业务bean*/
public class ServiceBean implements Service {
	private daoBean daobean;
	
	public ServiceBean(daoBean daobean) {
		super();
		this.daobean = daobean;
	}


	public ServiceBean() {
	}


	 public daoBean getDaobean() {
	 return daobean;
	 }
	
	
	 public void setDaobean(daoBean daobean) {
	 this.daobean = daobean;
	 }

	@Override
	public void save() {
		daobean.add();// 直接调用被注入的对象
	}

}
<!-- 构造器注入 -->
	<bean id="daoBean2" class="com.yiki.daobean.daoBean"></bean>
	<bean id="serviceBean2" class="com.yiki.bean.imp.ServiceBean">
	<constructor-arg index="0" type="com.yiki.daobean.daoBean" ref="daoBean2"></constructor-arg>
	</bean>

注入集合

package com.yiki.bean.imp;

import java.util.HashSet;
import java.util.Set;

public class SetBean {
	private Set<String> sets = new HashSet<String>();

	public Set<String> getSets() {
		return sets;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public void save() {
		System.out.println("set集合注入");

	}

}
<?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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />

	<!-- 【id】唯一,不能包含特殊字符 ,【name】允许特殊字符 -->

	<!-- [1]类构造器实例化 -->
	<bean id="service" class="com.yiki.bean.imp.ServiceBean" scope="prototype"></bean>

	<!-- [2]静态工厂 -->
	<bean id="serviceFactory" class="com.yiki.bean.imp.ServiceBeanFactory"
		factory-method="creatBean" lazy-init="false"></bean>

	<!-- [3]实例化工厂 -->
	<bean id="serviceFactory2" class="com.yiki.bean.imp.ServiceBeanFactory"></bean>
	<bean id="service2" factory-bean="serviceFactory2" factory-method="creatBean2"></bean>

	<!-- 依赖注入开始 【ref】被注入的bean的名称 -->
	<!-- set -->
	<bean id="daoBean" class="com.yiki.daobean.daoBean"></bean>
	<bean id="serviceBean" class="com.yiki.bean.imp.ServiceBean">
		<property name="daobean" ref="daoBean"></property>
	</bean>
	<!-- 依赖注入结束 -->
	
	<!-- 集合注入开始 -->
	<!-- Set集合 -->
	<bean id="setbean" class="com.yiki.bean.imp.SetBean">
	<property name="sets">
	<set><!-- 可以指定value的值 -->
	<value>第1</value>
	<value>第2</value>
	<value>第3</value>
	<value>第4</value>
	</set>
	</property>
	</bean>
	

</beans>





猜你喜欢

转载自blog.csdn.net/qq_38277033/article/details/80314392