Spring中的依赖注入

来源:互联网 发布:网络安全 书 知乎 编辑:程序博客网 时间:2024/05/17 22:33

Spring中的依赖注入

第一种:set方法来注入

首先创建一个类,类名为PersonServiceBean.java,代码为:

packagecn.csdn.hr.service;

importjava.util.Date;

publicclassPersonServiceBean {

 

    //封装属性

    private Stringname;

    private Stringsex;

    private Integerage;

    private Datebirth;

   

    publicPersonServiceBean() {

       super();

       //TODO Auto-generated constructor stub

    }

 

    publicPersonServiceBean(String name, String sex, Integer age, Date birth) {

       super();

       this.name = name;

       this.sex = sex;

       this.age = age;

       this.birth = birth;

    }

 

    //setter方法来依赖注入

    publicvoidsetName(String name) {

       this.name = name;

    }

 

    publicvoidsetSex(String sex) {

       this.sex = sex;

    }

 

    publicvoidsetAge(Integer age) {

       this.age = age;

    }

 

    publicvoidsetBirth(Date birth) {

       this.birth = birth;

    }

 

    @Override

    public String toString() {

       return"PersonServiceBean [name=" +name + ", sex=" + sex +", age="

              + age + ", birth=" +birth + "]";

    }

}

在xml中用set方法注入,xml的名字为beanset.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

    <beanid="personServiceBean"class="cn.csdn.hr.service.PersonServiceBean">

       <!--依赖注入的方式 -->

       <!--方式1:通过set注入 -->

       <propertyname="name">

           <value> redarmy </value>

       </property>

       <propertyname="sex">

           <value></value>

       </property>

       <propertyname="age">

           <value>28</value>

       </property>

       <!--出生日期 ref引用 -->

       <propertyname="birth"ref="date">

           <!--内部bean -->

           <!--<bean class="java.util.Date"></bean> -->

       </property>

    </bean>

   

    <!--日期bean的声明 set注入 -->

    <beanid="date"class="java.util.Date">

       <propertyname="year">

           <value>2011</value>

       </property>

       <propertyname="month">

           <value>11</value>

       </property>

       <propertyname="date">

           <value>1</value>

        </property>

    </bean>

</beans>

 

用构造器的方法注入:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

    <!--构造器注入在index=””属性值为空时,必须按顺序写,如果不按顺序写,按索引,在index=””按顺序写上属性值,从0开始,这样顺序可以换,属性必须全写,不全都写不对 -->

    <beanid="personServiceBean1"class="cn.csdn.hr.service.PersonServiceBean">

       <constructor-argindex="0">

           <value>redarmy</value>

       </constructor-arg>

       <constructor-argindex="">

           <value></value>

       </constructor-arg>

       <constructor-argindex="">

           <value>28</value>

       </constructor-arg>

       <constructor-argindex="">

           <!--内部bean,只在里面使用 -->

           <beanclass="java.util.Date"/>

       </constructor-arg>

    </bean>

    <beanid="personServiceBean"class="cn.csdn.hr.service.PersonServiceBean">

       <constructor-argtype="java.lang.String"value=" redarmy "/>

       <constructor-argtype="java.lang.String"value=""/>

       <constructor-argtype="java.lang.Integer"value="28"/>

       <constructor-argtype="java.util.Date"ref="date"/>

    </bean>

    <beanname="date"class="java.util.Date"></bean>

</beans>

 

测试的方法为:

    publicvoid test() {

       //第一步:获取应用程序上下文对象

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:beanset.xml");

   

       //第二步:根据应用程序上下文对象的gettBean

       PersonServiceBean personServiceBean = (PersonServiceBean) ac.getBean("personServiceBean");

       System.out.println(personServiceBean.toString());

    }

 

第二种:集合的注入

集合的注入的类名为TeacherServiceBean.java:

集合注入分为set、map、list和Properties

代码为:

Package cn.csdn.hr.service;

 

Importjava.util.List;

Importjava.util.Map;

importjava.util.Properties;

importjava.util.Set;

 

public class TeacherServiceBean {

 

    // 集合的注入

    private List<String> list;

    private Set<String> set;

    private Map<String, String> map;

 

    private Properties properties;

 

    publicTeacherServiceBean() {

       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;

    }

 

    @Override

    public String toString() {

       return "TeacherServiceBean [list=" + list + ", set=" + set + ", map="

              + map + "]";

    }

}

 

在xml中的配置,名字为bean2.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

    <!--集合的注入 -->

    <beanid="teacherServiceBean"class="cn.csdn.hr.service.TeacherServiceBean">

       <propertyname="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方式注入 -->

       <propertyname="set">

           <set>

              <value>计算机技术1</value>

              <value>计算机技术2</value>

              <value>计算机技术3</value>

              <value>计算机技术4</value>

              <value>计算机技术5</value>

           </set>

       </property>

 

 

       <!-- map集合的注入方式 -->

       <propertyname="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集合的配置 -->

       <propertyname="properties">

           <props>

              <propkey="0x001">redarmy1</prop>

              <propkey="0x002">redarmy 2</prop>

              <propkey="0x003"> redarmy 3</prop>

           </props>

       </property>

    </bean>

</beans>

 

获取写为构造器的注入方式:

<!--利用构造器的注入方式 -->

    <beanid="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>

              <propkey="0x001"> redarmy 1</prop>

              <propkey="0x002"> redarmy 2</prop>

              <propkey="0x003"> redarmy 3</prop>

           </props>

       </constructor-arg>

    </bean>

 

测试的方法为:

    publicvoidteacharTest(){

       ApplicationContext ac = newClassPathXmlApplicationContext("bean2.xml");

       TeacherServiceBeanteacherServiceBean = (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(Entrystr:mset){

           System.out.println(str.toString());

       }

      

       //prop集合

       Properties properties = teacherServiceBean.getProperties();

       Set<Entry<Object, Object>>pset = properties.entrySet();

       for(Entrystr:pset){

           System.out.println(str.toString());

       }

    }

原创粉丝点击