[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>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值