`
solitary
  • 浏览: 72947 次
社区版块
存档分类
最新评论

spring 注入map,set,list,property

    博客分类:
  • java
 
阅读更多

转自:http://blog.csdn.net/longyuan20102011/article/details/7596458

 

cn.dao

[java] view plain
copy

package cn.dao; 
 
public interface PersonDao { 
 
    public abstract void add(); 
 

cn.dao.imp


[java] view plain
copy

package cn.dao; 
 
public interface PersonDao { 
 
    public abstract void add(); 
 

cn.service


[java] view plain
copy

package cn.service; 
 
public interface PersonService { 
 
    public abstract void save(); 
 

cn.service.imp

[java] view plain
copy

package cn.service.imp; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 
import java.util.Set; 
 
import org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor; 
 
import cn.service.PersonService; 
 
public class PersonServiceBean implements PersonService { 
 
    private Set<String> sets=new HashSet<String>(); 
    private List<String> lists=new ArrayList<String>(); 
    private Properties properties=new Properties(); 
    private Map<String, String> maps=new HashMap<String, String>(); 
    public Map<String, String> getMaps() { 
        return maps; 
    } 
 
    public void setMaps(Map<String, String> maps) { 
        this.maps = maps; 
    } 
 
    public Properties getProperties() { 
        return properties; 
    } 
 
    public void setProperties(Properties properties) { 
        this.properties = properties; 
    } 
 
    public List<String> getLists() { 
        return lists; 
    } 
 
    public void setLists(List<String> lists) { 
        this.lists = lists; 
    } 
 
    public Set<String> getSets() { 
        return sets; 
    } 
 
    public void setSets(Set<String> sets) { 
        this.sets = sets; 
    } 
 
    public void save() 
    { 
        System.out.println("执行save()方法"); 
    } 

 

 

junit.test


[java] view plain
copy

package junit.test; 
 
import org.junit.Test; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.AbstractApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
import cn.service.imp.PersonServiceBean; 
 
 
public class SpringTest  

    @Test  
    public void instanceSpring() 
    { 
        AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); 
        PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService"); 
         
        System.out.println("-------------set--------------"); 
        for(String value:personServiceBean.getSets()) 
        { 
            System.out.println(value); 
        } 
         
        System.out.println("-------------list--------------"); 
        for(String value:personServiceBean.getLists()) 
        { 
            System.out.println(value); 
        } 
         
        System.out.println("-------------properties--------------"); 
        for(Object key:personServiceBean.getProperties().keySet()) 
        { 
            System.out.println(key+"="+(String)personServiceBean.getProperties().getProperty((String)key)); 
        } 
         
        System.out.println("-------------map--------------"); 
        for(String key:personServiceBean.getMaps().keySet()) 
        { 
            System.out.println(key+"="+(String)personServiceBean.getMaps().get(key)); 
        } 
        ctx.close(); 
    } 

 

 

 beans.xml


[html] view plain
copy

<?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="personService" class="cn.service.imp.PersonServiceBean"> 
            <property name="sets"> 
                <set> 
                    <value>第一个set</value> 
                    <value>第二个set</value> 
                    <value>第三个set</value> 
                </set> 
            </property> 
            <property name="lists"> 
                <list> 
                    <value>第一个list</value> 
                    <value>第二个list</value> 
                    <value>第三个list</value> 
                </list> 
            </property> 
            <property name="properties"> 
                <props> 
                    <prop key="key1">value1</prop> 
                    <prop key="key2">value2</prop> 
                    <prop key="key3">value3</prop> 
                </props> 
            </property> 
            <property name="maps"> 
                <map> 
                    <entry key="key1" value="value1"></entry> 
                    <entry key="key2" value="value2"></entry> 
                    <entry key="key3" value="value3"></entry> 
                </map> 
            </property> 
        </bean> 
 
</beans> 

分享到:
评论

相关推荐

    第四章 Spring4 注入参数

    Spring4也支持List、Set、Map等集合类型的注入。例如,我们可以注入一个包含多个Bean的列表: ```xml &lt;property name="listOfItems"&gt; &lt;list&gt; &lt;value&gt;Item1 &lt;value&gt;Item2 &lt;/list&gt; &lt;/property&gt; ``` 在Java...

    Spring (bean怎样注入值)学习实例

    本文将深入探讨如何在Spring中通过XML配置文件对Bean进行值的注入,包括List、Set和Map等集合类型的注入。 首先,我们需要了解Spring Bean的定义。在Spring中,Bean是一个被Spring容器管理的对象,它可以通过XML、...

    spring各种属性的注入

    虽然这里使用的是`&lt;list&gt;`标签,但实际上Spring会自动将其转换为数组类型注入。 4. **Map** 类型注入: ```xml &lt;property name="mapValue"&gt; &lt;map&gt; &lt;/map&gt; &lt;/property&gt; ``` 这里定义了一个键值对映射...

    spring 注入原理

    对于复杂类型的属性,如List、Map等,Spring提供了更高级的处理方式。例如,对于Map的注入,Spring会解析`&lt;map&gt;`标签下的`&lt;entry&gt;`元素,构建相应的Map结构,并通过Bean的setter方法注入。 #### 四、Spring的核心...

    基于spring的sql map实现

    "基于Spring的SQL Map实现"意味着我们将利用Spring的IoC(Inversion of Control)容器来管理SQL Map实例,以及通过Spring的AOP(Aspect-Oriented Programming)来处理事务。Spring的SqlSessionFactoryBean可以用来...

    Spring中的参数注入.pdf

    Spring支持注入集合类型的数据,包括List、Set、Map和Properties等。针对不同类型集合,Spring提供了不同的XML元素,如&lt;list/&gt;、&lt;set/&gt;、&lt;map/&gt;以及。 #### 3.1 list元素注入 &lt;list/&gt;元素用于注入List类型的集合...

    Spring中的结合配置

    在Spring框架中,集合配置是将Java集合对象如List、Set和Map与IoC容器集成的关键部分。这些配置允许我们动态地注入集合数据,为应用提供灵活的数据结构。本篇文章将详细探讨如何在Spring中配置这三种类型的集合。 ...

    Spring_Spring_教程8_注入_复杂类型的注入

    Spring支持对List、Set、Map等集合类型的注入。例如,你可以声明一个Bean,其属性是一个List,并在配置文件或使用Java配置类时提供具体的元素值。这使得在运行时,Spring会自动创建对应的集合并填充数据。 2. ...

    Spring - -setter方式 向bean中注入各种类型的值

    4. **集合类型的注入**:对于List、Set、Map等集合类型,Spring可以批量注入多个值。在XML配置中,你可以使用`&lt;list&gt;`、`&lt;set&gt;`或`&lt;map&gt;`标签来定义这些集合,并提供多个子元素。在Java配置中,可以使用`@Autowired`...

    一步步实现Spring框架(二)XML注入

    7. **Property注入**:对于复杂类型,如Properties,可以使用`&lt;props&gt;`元素: ```xml &lt;property name="myProps"&gt; &lt;prop key="key1"&gt;value1 &lt;prop key="key2"&gt;value2 &lt;/property&gt; ``` 了解了以上XML...

    spring集合属性

    在Spring框架中,集合属性(Collections Property)是一个重要的概念,它允许我们配置bean的属性为集合类型,如List、Set、Map等。这些集合可以由Spring容器动态填充,提供了极大的灵活性和可配置性,使得我们可以...

    Spring对集合的装配(各种集合类型的属性的注入方式)

    在Spring框架中,集合装配是将一组对象注入到如List、Set、Map等集合类型属性中的过程。这个过程是依赖注入(Dependency Injection,DI)的一个重要方面,它使得应用程序更加灵活,易于测试和维护。本篇文章将深入...

    day38 14-Spring的Bean的属性的注入:集合属性的注入

    在本主题“day38 14-Spring的Bean的属性的注入:集合属性的注入”中,我们将深入探讨如何向Bean注入集合类型的属性,如List、Set、Map等。这在实际开发中非常常见,因为很多情况下我们需要处理一组相关的数据。 ...

    spring-集合注入、自定义转换器

    集合注入允许我们在Spring配置中将一组对象注入到单个bean属性中,这些对象通常以集合类型(如List、Set、Map等)存在。这样做的好处是可以方便地管理多个依赖项,而无需为每个依赖项创建单独的bean。 例如,假设...

    Spring学习笔记之二“属性注入”

    对于集合类型的属性,如List、Set、Map等,Spring也提供了相应的注入方式。在XML配置中,可以通过`&lt;list&gt;`、`&lt;set&gt;`、`&lt;map&gt;`等标签实现;在注解方式中,可以使用`@Autowired`配合`@Resource`注解的`mapKey`或`list...

    尚学堂_Spring_0700_IOC_Collections

    在这个教程或博文中,我们可能将深入理解Spring如何管理和装配集合对象,如List、Set、Map等。这在实际开发中非常重要,因为集合经常被用来存储和处理一组相关的对象。 Spring IoC容器是Spring框架的核心部分,它...

    Spring中属性注入详解

    对于`List`、`Set`和`Map`等集合类型的注入,Spring同样支持。例如,注入listValue、setValue和mapValue: ```xml &lt;!-- ... --&gt; &lt;property name="listValue"&gt; &lt;list&gt; &lt;value&gt;Item1 &lt;value&gt;Item2 &lt;/list&gt; &lt;/...

    SSHnote_Spring基本配置

    Spring提供了多种方式来配置这些集合,如`&lt;list&gt;`、`&lt;set&gt;`、`&lt;map&gt;`和`&lt;props&gt;`元素。例如,你可以这样配置一个包含多个bean的列表:`&lt;list&gt;&lt;ref bean="bean1"/&gt;&lt;ref bean="bean2"/&gt;&lt;/list&gt;`。 Properties属性处理...

Global site tag (gtag.js) - Google Analytics