Spring装配集合属性

简介:

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

package chapter22;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
    private List myList;
    private String myArray[];
    private Set mySet;
    private Map myMap;
    private Properties myProperties;
    
    public List getMyList() {
        return myList;
    }
    public void setMyList(List myList) {
        System.out.println("set List类型:"+myList.getClass().getName());
        this.myList = myList;
    }
    public String[] getMyArray() {
        return myArray;
    }
    public void setMyArray(String[] myArray) {
        this.myArray = myArray;
    }
    public Set getMySet() {
        return mySet;
    }
    public void setMySet(Set mySet) {
        System.out.println("set Set类型:"+mySet.getClass().getName());
        this.mySet = mySet;
    }
    public Map getMyMap() {
        return myMap;
    }
    public void setMyMap(Map myMap) {
        System.out.println("set Map类型:"+myMap.getClass().getName());
        this.myMap = myMap;
    }
    public Properties getMyProperties() {
        return myProperties;
    }
    public void setMyProperties(Properties myProperties) {
        System.out.println("set Properties类型:"+myProperties.getClass().getName());
        this.myProperties = myProperties;
    }
}
下面是装配上述4中集合类型属性的配置代码
xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="helloservice" class="chapter22.HelloServiceImpl">
        <property name="greeting" value="greeting Xu Wei">property>bean>
    <bean id="myBean" class="chapter22.MyBean">
        <property name="hello">
            <ref bean="helloservice" />property>
        <property name="strName" value="Xu Wei">property>bean> <bean id="CollectionBean" class="chapter22.CollectionBean"> <property name="myList"> <list><value>abcdvalue> <idref bean="myBean">idref> list> property><property name="myMap"> <map> <entry> <key> <value>hellovalue>key> <value>1234value>entry> <entry key="abcd"> <ref bean="helloservice" />entry> <entry> <key> <ref bean="helloservice" >ref>key> <ref bean="myBean" >ref>entry>map>property> <property name="mySet"> <set> <value>testvalue> <ref bean="myBean">ref>set>property> <property name="myProperties"> <props><prop key="abcd">value1prop> <prop key="prop">myPropprop>props>property> <property name="myArray"> <list> <value>myArrayvalue> <idref bean="helloservice" >idref>list>property>bean> beans>

下面的TestCollectionContext.java代码使用ApplicationContext接口的getBean方法创建了CollectionBean对象,并访问了相关属性。

package chapter22;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestCollectionBean {
    public static void main(String args[])
    {
        ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");
/*在创建ctx对象实例以后,applicationContext.xml配置中涉及到的Bean的set方法都被调用了。
 *单单执行ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");
 *的输出结果是:
 *log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
 *log4j:WARN Please initialize the log4j system properly.
 *setGreeting()方法被调用
 *setHello()方法被调用
 *setStrName()方法被调用
 *set List类型:java.util.ArrayList
 *set Map类型:java.util.LinkedHashMap
 *set Set类型:java.util.LinkedHashSet
 *set Properties类型:java.util.Properties
 */    
        //创建CollectionBean对象cb。
        CollectionBean cb=(CollectionBean)ctx.getBean("CollectionBean");
        
        System.out.println("------测试List------");
        for(String s:cb.getMyList())
            System.out.println(s);
        System.out.println("------测试Array------");
        for(String s:cb.getMyArray())
            System.out.println(s);
        System.out.println("------测试Set------");
        for(Object obj:cb.getMySet())
            System.out.println(obj.getClass().getName());
        System.out.println("------测试Map------");
        //Map其实就是key-value的键值对。cb.getMyMap().get(key)是要找到key所对应的value。
        for(Object key:cb.getMyMap().keySet())
            System.out.println(key+"="+cb.getMyMap().get(key));
        System.out.println("------测试Properties------");
        for(Object key:cb.getMyProperties().keySet())
            System.out.println(key+"="+cb.getMyProperties().get(key));
    }
}

输出结果:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.

prop=myProp





本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2010/09/12/2297093.html,如需转载请自行联系原作者


目录
相关文章
|
7月前
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
101 0
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
7月前
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
242 0
|
7月前
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
105 0
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
11月前
|
消息中间件 Java 数据库
解密Spring Boot:深入理解条件装配与条件注解
Spring Boot中的条件装配与条件注解提供了强大的工具,使得应用程序可以根据不同的条件动态装配Bean,从而实现灵活的配置和管理。通过合理使用这些条件注解,开发者可以根据实际需求动态调整应用的行为,提升代码的可维护性和可扩展性。希望本文能够帮助你深入理解Spring Boot中的条件装配与条件注解,在实际开发中更好地应用这些功能。
203 2
|
12月前
|
前端开发 Java Spring
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
【Spring】“请求“ 之后端传参重命名,传递数组、集合,@PathVariable,@RequestPart
214 2
|
XML Java 数据格式
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
这篇文章是Spring5框架的实战教程,主题是IOC容器中Bean的集合属性注入,通过XML配置方式。文章详细讲解了如何在Spring中注入数组、List、Map和Set类型的集合属性,并提供了相应的XML配置示例和Java类定义。此外,还介绍了如何在集合中注入对象类型值,以及如何使用Spring的util命名空间来实现集合的复用。最后,通过测试代码和结果展示了注入效果。
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
|
XML 前端开发 Java
Spring MVC接收param参数(直接接收、注解接收、集合接收、实体接收)
Spring MVC提供了灵活多样的参数接收方式,可以满足各种不同场景下的需求。了解并熟练运用这些基本的参数接收技巧,可以使得Web应用的开发更加方便、高效。同时,也是提高代码的可读性和维护性的关键所在。在实际开发过程中,根据具体需求选择最合适的参数接收方式,能够有效提升开发效率和应用性能。
311 3
|
XML 前端开发 Java
Spring MVC接收param参数(直接接收、注解接收、集合接收、实体接收)
Spring MVC提供了灵活多样的参数接收方式,可以满足各种不同场景下的需求。了解并熟练运用这些基本的参数接收技巧,可以使得Web应用的开发更加方便、高效。同时,也是提高代码的可读性和维护性的关键所在。在实际开发过程中,根据具体需求选择最合适的参数接收方式,能够有效提升开发效率和应用性能。
546 2