`
虎嗅蔷薇
  • 浏览: 7204 次
社区版块
存档分类
最新评论

spring 3配置文件中如何注入map list set等类型

    博客分类:
  • JAVA
阅读更多
首先写个 javabean类吧,如下

[java] view plaincopy
package com.bean; 
 
import java.util.List; 
import java.util.Map; 
import java.util.Properties; 
import java.util.Set; 
 
public class MessageBean { 
    private String username; 
    private String password; 
    private int size; 
    private List<String> citys; 
    private Set<String> friends; 
    private Map<Integer,String> books; 
    private Properties props; 
     
    public void setProps(Properties props) { 
        this.props = props; 
    } 
 
    public void setFriends(Set<String> friends) { 
        this.friends = friends; 
    } 
 
    public void setSize(int size) { 
        this.size = size; 
    } 
 
    public void setPassword(String password) { 
        this.password = password; 
    } 
 
    public void setUsername(String username) { 
        this.username = username; 
    } 
 
    public void show(){ 
        System.out.println(username); 
        System.out.println(password); 
        System.out.println(size); 
        System.out.println("----------"); 
        for(String str:citys){ 
            System.out.println(str); 
        } 
        System.out.println("----------"); 
        for(String str:friends){ 
            System.out.println(str); 
        } 
        System.out.println("---------"); 
        Set<Integer> keys = books.keySet(); 
        for(Integer key:keys){ 
            System.out.println(key+" "+books.get(key)); 
        } 
        System.out.println("---------"); 
        Set params = props.keySet(); 
        for(Object obj:params ){ 
            System.out.println(obj+" : " 
                    +props.getProperty(obj.toString())); 
        } 
    } 
 
    public void setCitys(List<String> citys) { 
        this.citys = citys; 
    } 
 
    public void setBooks(Map<Integer, String> books) { 
        this.books = books; 
    } 


配置文件中配置

[html] view plaincopy
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
 
<beans> 
    <bean id="userdao" 
         class="com.dao.impl.HibernateUserDAO"> 
    </bean> 
    <!-- setter方式注入 --> 
    <bean id="useraction" 
         class="com.action.UserAction"> 
         <property name="userDao" ref="userdao"> 
         </property> 
    </bean> 
    <!-- 构造器方式注入 --> 
    <bean id="useraction1" 
             class="com.action.UserAction1"> 
             <constructor-arg index="0" ref="userdao"/> 
    </bean> 
    <!-- 各种类型值注入的写法 --> 
    <bean id="messagebean"  
            class="com.bean.MessageBean"> 
            <property name="props"> 
                <props> 
                    <prop key="url">http://www.tom.com</prop> 
                    <prop key="username">zhangsan</prop> 
                    <prop key="password">123456789</prop> 
                </props> 
            </property> 
             
            <property name="books"> 
                <map> 
                    <entry key="10" value="CoreJava"> 
                    </entry> 
                    <entry key="11" value="JavaWeb"> 
                    </entry> 
                    <entry key="12" value="SSH2"> 
                    </entry> 
                </map> 
            </property> 
            <property name="friends"> 
                <set> 
                    <value>张三</value> 
                    <value>李四</value> 
                    <value>王五</value> 
                </set> 
            </property> 
             
            <property name="citys"> 
                <list> 
                    <value>北京</value> 
                    <value>上海</value> 
                    <value>深圳</value> 
                </list> 
            </property> 
             
            <property name="username"> 
                <value>root</value> 
            </property> 
            <property name="password"> 
                <value>1234</value> 
            </property> 
            <property name="size"> 
                <value>15</value> 
            </property> 
    </bean> 
     
</beans> 

代码中调用

[java] view plaincopy
String[] configs = {"applicationContext.xml"}; 
    ApplicationContext ac = 
        new ClassPathXmlApplicationContext(configs); 
    MessageBean msgBean =  
        (MessageBean)ac.getBean("messagebean"); 
    msgBean.show(); 
分享到:
评论

相关推荐

    Spring_集合(List_Map_Set)_自动装配

    Spring实现集合(List_Map_Set)_自动装配,适合初学者熟悉集合类型的装配。

    Spring的一些配置信息(date map and so on)

    Spring的一些配置信息(date map and so on) Spring Date Map List Set Properties 自己写了一个属性编辑器,主要对Date型数据进行操作

    带有外部定义集合命名空间的Spring配置文件

    该xml文件不仅仅是Spring框架初次使用时的主配置文件,而且该文件里面包含了java.util里面的List,Set,Map,Properties等的命名空间,支持在配置文件中定义外部的集合,便于参数注入的复用性

    List<Map>转化为List工具类

    一般使用springjdbc、hibernate的sql查询,库获取到的数据都是List&lt;Map, Object&gt;&gt;结果集,如果我们要转化为JavaBean,则需要做一系列的map.get(),然后obj.set()。 此工程中就是解决List&lt;Map, Object&gt;&gt;转化为...

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

    实例主要讲述了,Spring的Xml(list,set,map)怎样进行注入值

    java面试宝典

    3、String 是最基本的数据类型吗? 8 4、float 型float f=3.4是否正确? 8 5、语句float f=1.3;编译能否通过? 8 6、short s1 = 1; s1 = s1 + 1;有什么错? 8 7、Java 有没有goto? 8 8、int 和Integer 有什么区别? 9 9...

    Spring中的结合配置

    这里是些我在学习Spring中总结的一些集合的配置

    spring.doc

    2.2 Spring配置文件 7 2.3 Spring API 8 3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认...

    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框架(二)XML注入

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

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    Java代码实现依赖注入

    模仿Spring实现一种基于xml配置文件的...文件中将实现3中注入,一是单值注入,包括int,float,double,char等,也包括String注入;二是Java容器注入,包括List,Set,Map三种容器的注入,最后一种是java bean对象注入。

    springboot 的配置文件加载顺序

    springboot 的配置文件加载顺序 ... 本系列校训 用免费公开视频,卷飞培训班哈人!打死不报班,赚钱靠狠干!...YAML 对象、数组、List、Map、Set的行内写法与缩进写法 YAML 位置 第一个位置 第二个位置 其它位置

    spring-data-redis支持批量操作

    public List&lt;V&gt; pipelineGet(Set&lt;K&gt; keys){ return rt.opsForPipeline().get(keys); } public void pipelineSet(Map,V&gt; valueMap){ redisTemplate.opsForPipeline().set(valueMap); } public void ...

    spring 高性能 代码

    spring的良好的扩展性,集成度,IOC,AOP事务,已经是项目的基础条件. 整个项目只使用了spring 没有struts,没有hibernate //就极简而言,一个数据库只需要一个Service,就可以查询这个数据库的任意一张表 //以下是我的...

    redis与spring的整合

    只是理解一下redis与spring整合的大概步骤,包括String,list,set,map格式的值

    Spring装配集合属性

    在Spring中可以装配4种集合类型属性:List、set、Map和Properties。与这四种集合对应的标签是、、、。CollectionBean是一个包含上述4种集合类型的JavaBean,代码如下:

    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已经默认...

    初识Spring

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

    装配bean—集合类型注入值源码

    装配bean——集合类型注入值: 本文介绍数组、list集合、set集合、map集合、properties的注值 博客原文地址:http://blog.csdn.net/tingzhiyi/article/details/52104203

Global site tag (gtag.js) - Google Analytics