Spring Inner bean,List,map,set的xml配置

Spring Inner bean,List,map,set的xml配置

java程序(接口)
package com.java.test;

/**
 * 定义输出输出接口
 */
public interface ICollectionTest {

    /**
     * 输出
     */
    public void printCollection();
}
java程序(实现类)
package com.java.test.property;

public class CollectionTest02 {
    public  String printCollection(){
        return "this is a inner bean";
    }
}

XML配置信息(bean.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="collectionTest" class="com.java.test.property.CollectionTest">
        <!-- inner bean name:对象名称 bean下定义只有Class属性,没有name属性-->
        <property name="collectionTest02">
            <bean class="com.java.test.property.CollectionTest02"></bean>
        </property>
        <!-- list-->
        <property name="demoList">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list2</value>
            </list>
        </property>
        <!-- map -->
        <property name="maps">
            <map>
                <entry key="entryKey1">
                    <value>entryValue1</value>
                </entry>
                <entry key="entryKey2">
                    <value>entryValue2</value>
                </entry>
                <entry key="entryKey3">
                    <value>entryValue3</value>
                </entry>
            </map>
        </property>
        <!-- set -->
        <property name="sets">
           <set>
               <value>set1</value>
               <value>set2</value>
               <value>set3</value>
           </set>
        </property>
    </bean>
</beans>
测试类
import com.java.test.ICollectionTest;
import com.java.test.property.CollectionTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        ICollectionTest ICollectionTest = context.getBean("collectionTest", CollectionTest.class);
        ICollectionTest.printCollection();
    }
}
测试结果
this is a inner bean
list1
list2
list2
entryKey1:entryValue1
entryKey2:entryValue2
entryKey3:entryValue3
set1
set2
set3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值