Spring中依赖注入的几种方式

来源:互联网 发布:截面数据用什么模型 编辑:程序博客网 时间:2024/05/01 05:59

一、依赖注入

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);}}

0 0
原创粉丝点击