Spring中依赖注入的几种方式

一、依赖注入

1.属性注入,要求在javabean中提供setter方法,如下:

1)配置文件applicationContext.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-3.1.xsd">
	<bean id="userinfo" class="com.casv.entity.userentity">
	    <property name="name" value="tomact"/>
	    <property name="sex" value="man"/>
	    <property name="age" value="49"/>
	</bean>
</beans>
2)实体类userentity.java

package com.casv.entity;

public class userentity {

	private String name;
	private String sex;
	private String age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	public void info(){
		System.out.println("name:"+name+",sex:"+sex+",age:"+age);
	}
}
3)testapp.java测试

public void test(){
		//读取applicationContext.xml配置文件
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取bean实例
		userentity user=(userentity) context.getBean("userinfo");
		user.info();
	}
2.构造器注入, 要求在javabean中提供带参数的构造方法,如下:

1)配置文件applicationContext.xml,可以按类型(type)与索引(index)匹配入参

<?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-3.1.xsd">
	<bean id="userinfo" class="com.casv.entity.userentity">
		<constructor-arg type="java.lang.String" index="0" value="周星驰"/>  
		<constructor-arg type="java.lang.String" index="1" value="男"/>
		<constructor-arg type="java.lang.String" index="2" value="45"/>
	</bean>
</beans>

二、注入参数

1.注入常量

<bean id="userinfo" class="com.casv.entity.userentity">
	    <property name="name" value="周星驰"/>
	    <property name="sex" value="男"/>
	    <property name="age" value="50"/>
</bean>
2.注入List

1)创建实体类project.java,属性为list类型

public class Project {

	private List prolist;

	public List getProlist() {
		return prolist;
	}

	public void setProlist(List prolist) {
		this.prolist = prolist;
	}
	
	public void run(){
		System.out.println("项目:"+prolist);
	}
	
}

2)在配置文件applicationContext.xml中注入参数

<bean id="projectID" class="com.casv.entity.Project">
	   <property name="prolist">
	      <list>
	         <value>协同办公系统</value>
	         <value>在线考试系统</value>
	         <value>企业云平台caecp</value>
	      </list>
	   </property>
</bean>

3)test测试

public void test1(){
	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	Project pro=(Project) context.getBean("projectID");
	List prolist=pro.getProlist();
	//遍历List中的元素
	for(int i=0;i<prolist.size();i++){
	   System.out.println("项目"+i+":"+prolist.get(i));
	}
		
}
3.注入Map

1)创建实体类Country.java,属性为Map类型

public class Country {
	
	private Map countrymap;

	public Map getCountrymap() {
		return countrymap;
	}

	public void setCountrymap(Map countrymap) {
		this.countrymap = countrymap;
	}

}
2)在配置文件applicationContext.xml中注入参数

<bean id="countryID" class="com.casv.entity.Country">
    <property name="countrymap">
	 <map>
	      <entry key="zhongguo" value="中国"></entry>
	      <entry key="meiguo" value="美国"></entry>
	 </map>
     </property>
</bean>

3)test测试

public void test(){
	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	Country cname=(Country) context.getBean("countryID");
	Map map=cname.getCountrymap();
	Set<String> set=map.keySet();
	Iterator<String> it=set.iterator();
	while(it.hasNext()){
		String key=it.next();
		String value=(String) map.get(key);
		System.out.println(value);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值