您的位置:首页 > 编程语言 > Java开发

Spring中依赖注入的几种方式

2016-12-22 16:48 519 查看
一、依赖注入

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring