spring利用set操作对引用类型和集合类型进行注入值

来源:互联网 发布:linux怎么删除目录 编辑:程序博客网 时间:2024/03/29 09:46

下面说说利用set对对象进行注入值。下面举个例子:

1.对引用对对象如何注入值呢?

首先写一个person类作为引用类型

Person.Java

packagecom.fish;

 

public class Person {

Stringname ;

Stringid;

 

publicString getName() {

    return name;

}

public void setName(String name) {

    this.name = name;

}

publicString getId() {

    return id;

}

public void setId(String id) {

    this.id = id;

}

 

}

 

然后写一个对person的引用类

packagecom.fish;

 

 

public class Student {

Personperson;

int age;

publicPerson getPerson() {

    return person;

}

public void setPerson(Person person) {

    this.person = person;

}

public int getAge() {

    return age;

}

public void setAge(int age) {

    this.age = age;

}

 

}

 

那么在spring.XML

方法一: <beanid="fish"class="com.fish.Person">

<propertyname="id"value="11"></property>//首先对person对象注入值,idname是基本对象。Name写的是person里面的属性,value是你想注入的值

        <propertyname="name"value="yaku"></property>//同理

    </bean>

<beanid="bulefish"class="com.fish.Student">//然后对student类的实例化

<propertyname="age"value="13"></property>

<propertyname="person"ref="fish"></property>//如果是引用对象就ref属性,来获取值ref里面写的是你想引用的对象的名字。和上面的fish是对应的。

</bean>

方法二:在bean的内部注入:

<beanid="redfish"class="com.fish.Student">

        <propertyname="person">//对人这个属性赋值

            <beanclass="com.fish.Person">//首先关联这个类

                <propertyname="id"value="16"></property>//然后按正常赋值就行

                <propertyname="name"value="yayc"></property>

            </bean>

        </property>

    <propertyname="age"value="13"></property>

 

</bean>

这里内部注入就有点想内部匿名类一样,其他bean访问不到他。

那么我们写一个测试类:Test.JAVA

package com.fish;

 

importorg.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Test {

public static void main(String[] args) {

       ApplicationContextcontext=new ClassPathXmlApplicationContext("spring.xml");

       Studentstudent=(Student)context.getBean("redfish");

       System.out.println(student.getAge()+""+student.getPerson().getId()+""+student.getPerson().getName());

     

}

 

}

我们就可以得到我们注入的值:结果是13 16 yaku

第二:对集合对象的注入,list,map和属性对象的注入。

我们就在原有的基础上拓展吧。

首先在student类里面加上这些属性

List<String>list;

Propertiesproperties;

Map<String,String> map;

 

publicProperties getProperties() {

    return properties;

}

public void setProperties(Properties properties) {

    this.properties = properties;

}

publicMap<String, String> getMap() {

    return map;

}

public void setMap(Map<String, String> map) {

    this.map = map;

}

publicList<String> getList() {

    return list;

}

public void setList(List<String> list) {

    this.list = list;

}

 

然后在spring.xml写上

<beanid="redfish"class="com.fish.Student">

        <propertyname="person">

            <beanclass="com.fish.Person">

                <propertyname="id"value="16"></property>

                <propertyname="name"value="yayc"></property>

            </bean>

        </property>

        <propertyname="age"value="12"></property>

        <propertyname="list">  //这是对list的注入首先name里面的值依旧是填上我们类中对应的属性,下面也一样

            <list>     //spring有标签对lst集合进行赋值的。

 

                <value>a</value>

                <value>b</value>

                <value>c</value>

                <value>d</value>

            </list>//这段话就是讲abcd放入 list集合中

        </property>

        <propertyname="map">   //同理map也是有有标签的

            <map>

                <entrykey="q"value="zzzz"></entry>  //存放按键值对形式。

                <entrykey="w"value="xxxx"></entry>

                <entrykey="e"value="cccc"></entry>

                <entrykey="r"value="vvvv"></entry>

 

            </map>

        </property>

       

        <propertyname="properties">   //properties也是一样的。

        <props>

        <propkey="b">aaa</prop>

        <propkey="n">bbb</prop>

        <propkey="m">ccc</prop>

       

        </props>

        </property>

    </bean>

 

下面对这些注入的值进行读取吧:

我们在test.Java在写上

for(String i:student.getList()){

          System.out.println(i);

          }

          for(String i:student.getMap().keySet()){

          System.out.println(i+"="+student.getMap().get(i));

          

          }

         

          for(Object i:student.getProperties().keySet()){

          System.out.println(i+"="+student.getProperties().getProperty((String) i));

          

          }

 

这样我们就可以获取我们需要的值了。利用增强for循环对值的遍历。

原创粉丝点击