Spring复杂属性的注入案例

Spring复杂属性的注入

目录结构

di
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - CollectionBean.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml

详细代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <groupId>com.zhangxin9727</groupId>
    <artifactId>di</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
        </dependency>
    </dependencies>
</project>

CollectionBean.java

package com.zhangxin9727;

import java.util.*;

public class CollectionBean {
    private String[] arrays; //数组
    private List<String> list; //List
    private Set<String> set; //Set
    private Map<String, Integer> map; //Map
    private Properties properties; //Properties

    public void setArrays(String[] arrays) {
        this.arrays = arrays;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "CollectionBean{\n" +
                "arrays=" + Arrays.toString(arrays) +
                ",\nlist=" + list +
                ",\nset=" + set +
                ",\nmap=" + map +
                ",\nproperties=" + properties +
                "\n}";
    }
}

applicationContext.xml

<?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.xsd">

    <bean id="collection" class="com.zhangxin9727.CollectionBean">
        <!--数组-->
        <property name="arrays">
            <list>
                <value>Array1</value>
                <value>Array2</value>
                <value>Array2</value>
            </list>
        </property>
        <!--List-->
        <property name="list">
            <list>
                <value>List1</value>
                <value>List2</value>
                <value>List3</value>
            </list>
        </property>
        <!--Set-->
        <property name="set">
            <set>
                <value>Set1</value>
                <value>Set2</value>
                <value>Set3</value>
            </set>
        </property>
        <!--Map-->
        <property name="map">
            <map>
                <entry key="Map1" value="1"/>
                <entry key="Map2" value="2"/>
                <entry key="Map3" value="3"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="Properties1">1</prop>
                <prop key="Properties2">2</prop>
                <prop key="Properties3">3</prop>
            </props>
        </property>
    </bean>
</beans>

MyTest.java

import com.zhangxin9727.CollectionBean;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        CollectionBean collection = (CollectionBean) context.getBean("collection");
        System.out.println(collection);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值