Spring中的依赖注入

来源:互联网 发布:淘宝互刷流量 编辑:程序博客网 时间:2024/05/18 02:48
Spring中的依赖注入第一种:set方法来注入首先创建一个类,类名为PersonServiceBean.java,代码为:package cn.csdn.hr.service;import java.util.Date;public class PersonServiceBean {//封装属性private String name;private String sex;private Integer age;private Date birth;public PersonServiceBean() {super();// TODO Auto-generated constructor stub}public PersonServiceBean(String name, String sex, Integer age, Date birth) {super();this.name = name;this.sex = sex;this.age = age;this.birth = birth;}//setter方法来依赖注入public void setName(String name) {this.name = name;}public void setSex(String sex) {this.sex = sex;}public void setAge(Integer age) {this.age = age;}public void setBirth(Date birth) {this.birth = birth;}@Overridepublic String toString() {return "PersonServiceBean [name=" + name + ", sex=" + sex + ", age="+ age + ", birth=" + birth + "]";}}在xml中用set方法注入,xml的名字为beanset.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"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean"><!-- 依赖注入的方式 --><!--方式1 : 通过set注入 --><property name="name"><value>laowang</value></property><property name="sex"><value>女</value></property><property name="age"><value>23</value></property><!-- 出生日期 ref引用 --><property name="birth" ref="date"><!-- 内部bean --><!-- <bean class="java.util.Date"></bean> --></property></bean><!-- 日期bean的声明  set注入 --><bean id="date" class="java.util.Date"><property name="year"><value>2011</value></property><property name="month"><value>11</value></property><property name="date"><value>1</value></property></bean></beans>用构造器的方法注入:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 构造器注入 必须按顺序写,如果不按顺序写,按索引,从0开始,这样顺序可以换,属性必须全写,不全都写不对 --><bean id="personServiceBean1" class="cn.csdn.hr.service.PersonServiceBean"><constructor-arg index="0"><value>老王</value></constructor-arg><constructor-arg><value>女</value></constructor-arg><constructor-arg><value>23</value></constructor-arg><constructor-arg><!-- 内部bean,只在里面使用 --><bean class="java.util.Date" /></constructor-arg></bean><bean id="personServiceBean" class="cn.csdn.hr.service.PersonServiceBean"><constructor-arg type="java.lang.String" value="老王" /><constructor-arg type="java.lang.String" value="男" /><constructor-arg type="java.lang.Integer" value="23" /><constructor-arg type="java.util.Date" ref="date" /></bean><bean name="date" class="java.util.Date"></bean></beans>测试的方法为:public void test() {//第一步:获取应用程序上下文对象ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");//第二步:根据应用程序上下文对象的gettBeanPersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");System.out.println(personServiceBean.toString());}第二种:集合的注入普通的注入集合的注入的类名为TeacherServiceBean.java:集合注入分为set、map、list和Properties代码为:package cn.csdn.hr.service;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class TeacherServiceBean {// 集合的注入private List<String> list;private Set<String> set;private Map<String, String> map;private Properties properties;public TeacherServiceBean() {super();// TODO Auto-generated constructor stub}public TeacherServiceBean(List<String> list, Set<String> set,Map<String, String> map, Properties properties) {super();this.list = list;this.set = set;this.map = map;this.properties = properties;}public void setList(List<String> list) {this.list = list;}public void setSet(Set<String> set) {this.set = set;}public void setMap(Map<String, String> map) {this.map = map;}// 用户测试public List<String> getList() {return list;}public Set<String> getSet() {return set;}public Map<String, String> getMap() {return map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="+ map + "]";}}在xml中的配置,名字为bean2.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"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 集合的注入 --><bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean"><property name="list"><list><value>hp电脑1</value><value>hp电脑2</value><value>hp电脑3</value><value>hp电脑4</value><value>hp电脑5</value><value>hp电脑6</value></list></property><!-- set方式注入 --><property name="set"><set><value>计算机技术1</value><value>计算机技术2</value><value>计算机技术3</value><value>计算机技术4</value><value>计算机技术5</value></set></property><!-- map集合的注入方式 --><property name="map"><map><entry><key><value>0001</value></key><value>java编程与开发1</value></entry><entry><key><value>0002</value></key><value>java编程与开发2</value></entry><entry><key><value>0003</value></key><value>java编程与开发3</value></entry></map></property><!-- prop集合的配置 --><property name="properties"><props><prop key="0x001">老王1</prop><prop key="0x002">老王2</prop><prop key="0x003">老王3</prop></props></property></bean></beans>获取写为构造器的注入方式:<!-- 利用构造器的注入方式 --><bean id="teacherServiceBean1" class="cn.csdn.hr.service.TeacherServiceBean"><constructor-arg><list><value>hp电脑1</value><value>hp电脑2</value><value>hp电脑3</value><value>hp电脑4</value><value>hp电脑5</value><value>hp电脑6</value></list></constructor-arg><constructor-arg><set><value>计算机技术1</value><value>计算机技术2</value><value>计算机技术3</value><value>计算机技术4</value><value>计算机技术5</value></set></constructor-arg><constructor-arg><map><entry><key><value>0001</value></key><value>java编程与开发1</value></entry><entry><key><value>0002</value></key><value>java编程与开发2</value></entry><entry><key><value>0003</value></key><value>java编程与开发3</value></entry></map></constructor-arg><constructor-arg><props><prop key="0x001">老王1</prop><prop key="0x002">老王2</prop><prop key="0x003">老王3</prop></props></constructor-arg></bean>测试的方法为:public void teacharTest(){ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");TeacherServiceBean teacherServiceBean = (TeacherServiceBean) ac.getBean("teacherServiceBean1");//list集合的注入方式List<String> list = teacherServiceBean.getList();for(String str:list){System.out.println(str.toString());}//set集合的注入方式Set<String> set = teacherServiceBean.getSet();for(String str:set){System.out.println(str.toString());}//map集合的注入方式Map<String, String> map = teacherServiceBean.getMap();Set<Entry<String,String>> mset = map.entrySet();for(Entry str:mset){System.out.println(str.toString());}//prop集合Properties properties = teacherServiceBean.getProperties();Set<Entry<Object, Object>> pset = properties.entrySet();for(Entry str:pset){System.out.println(str.toString());}}使用spring-util来完成集合的注入在用util注入之前要先导入需要的xsd文件,先引入http://www.springframework.org/schema/util然后引入http://www.springframework.org/schema/util/spring-util-2.5.xsd最终的格式为:<beans xmlns="http://www.springframework.org/schema/beans"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/util       http://www.springframework.org/schema/util/spring-util-2.5.xsd"></beans>这样就可以使用util的工具了,使用的方法如下:<bean id="teacherServiceBean" class="cn.csdn.hr.service.TeacherServiceBean"><!-- list集合的注入 --><property name="list"><util:list><value>java学习语言</value></util:list></property><!-- set集合的注入 --><property name="set"><util:set><value>set集合的注入</value></util:set></property><!-- map集合的注入 --><property name="map"><util:map><entry key="0x0001" value="map集合的注入"></entry></util:map></property><!-- properties的注入 --><property name="properties"><util:properties><prop key="0xxx">00java</prop></util:properties></property></bean>空值的注入,只要在属性里写:<property name="birth"><null/></property>就可以了