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

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> 

分享到:
评论

相关推荐

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

    实现了XML注入Bean,为bean注入bean,构造注入,Map,List,Set,Property 注入

    05spring4_di.rar

    &lt;beans xmlns="http://www.springframework.org/schema/beans" ...-- p命名空间注入属性依然要设置set方法 --&gt; 风清扬" p:age="230"/&gt; &lt;!--c命名空间注入要求有对应参数的构造方法 --&gt; &lt;/beans&gt;

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring原理模拟代码

    String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m....

    springmybatis

    public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserAge()...

    初识Spring

    (1)setter方法注入:(2)调用带参的构造方式注入:(3)p名称空间注入:(4)spel表达式注入:复杂类型注入(1)List是一种按照序号标识的集合(2)Set与List相似但是元素不允许重复(3)Map则是一种自定的键值对...

    thymeleaf-extras-springsecurity-3.0-master.zip

    As with normal Spring EL expressions, Thymeleaf allows you to access a series of objects from them including the context variables map (the #vars object). In fact, you are allowed to surround your ...

    spring整合shiro

    -- &lt;property name="successUrl" value="/blog/drug/list"/&gt;--&gt; 06 &lt;property name="unauthorizedUrl" value="/unauthorized.jsp"/&gt; 07 &lt;!-- The 'filters' property is not necessary since any declared ...

    springmvcmybatis

    &lt;tx:method name="set*" propagation="REQUIRED" /&gt; *" propagation="REQUIRED" /&gt; *" propagation="REQUIRED" /&gt; *" propagation="REQUIRED" /&gt; *" propagation="REQUIRED" read-only="true" /&gt; *" ...

    Bootstrap分页

    List list = this.userService.getUserLists(map); this.initResult(model, list, map); return new ModelAndView("pagerList"); } catch (Exception e) { e.printStackTrace(); } return ...

    基于Java的ORM框架Mongodb-ORM.zip

     &lt;property column="name" name="name" operate="set" /&gt;  &lt;property column="status" operate="set" /&gt;   有嵌套的查询 id="queryModelList3" collection="test_sample"&gt;   class="java.lang.String...

    cms后台管理

    protected List&lt;Content&gt; getList(Map, TemplateModel&gt; params, Environment env) throws TemplateException { Integer[] ids = DirectiveUtils.getIntArray(PARAM_IDS, params); if (ids != null) { //根据内容...

    java面试题

    list、set、map问题? 答:set 不允许重复,无序 list 允许重复,有序 map 成对的数据结构,键值必须具有唯一性 Servlet和CGI的区别? 答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式允许...

    从J2SE到J2EE知识点介绍

    4. 为Bean配置集合(List,Set,Map) 266 5. 调用构造方法创建Bean 271 6. 指定Bean引用与自动装配 276 (二) AOP 278 1. AOP简介 278 (三) Spring 3.X 新特性(JavaConfig,SpEL)简介 285 1. JavaConfig 285 2. SpEL...

    DWR.xml配置文件说明书(含源码)

    6、以上所有类型的集合,List,Set,Map(有些局限性) However nothing is added to the list of classes that can be created (i.e. put up for remoting) without you declaring it. 2、 The Converters DWR已经默认...

Global site tag (gtag.js) - Google Analytics