zoukankan      html  css  js  c++  java
  • Spring入门_02_属性注入

    Spring 的set方法(属性)注入

    UserAction类中设置属性和get、set方法。(实际上只需要set方法)

    private List list = null;
    private Set set = null;
    private Map map = null;
    private Properties props = null;
    //get、set方法省略。

    applicationContext.xml

    <bean id="userAction" class="com.umgsai.spring.UserAction" scope="singleton">
        <property name="manager">
            <ref bean="userManager">
        </property>
        <property name="list">
            <list>
                <value>123</value>
                <ref local="cur">
            </list>
        </property>
        <property name="set">
            <set>
                <value>Hello</value>
                <value>12.36</value>
                <value>124</value>
                <value type="java.lang.String">true</value><!--指定type-->
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="a1">
                    <value>umgsai</value>
                </entry>
                <entry key="a2">
                    <value>umgsai2</value>
                </entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="x1">as</prop>
                <prop key="x2">1265.3</prop>
            </props>
        </property> 
    </bean>

    MainClass.java

    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserAction ua = (UserAction)factory.getBean("userAction");
    for(Object o : ua.getList()){
        System.out.println(o);
    }
    for(Object o : ua.getSet()){
        System.out.println(o);
    }
    for(Iterator iter = ua.getMap().entrySet.iterator();iter.hasNext();){
        Entry entry = (Entry)iter.next();
        System.out.println(entry.getKey()+":"+entry.getValue()s);
    }


    本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1255528

  • 相关阅读:
    去掉[]中的英文字符
    Python面向对象OOP
    Python内置模块
    Python面向对象
    Python文件操作
    Python元组数据类型详解
    Jenkins+postman发送邮件测试报告及附件
    Python列表数据类型详解
    Python面向对象高阶描述符与设计魔术方法
    Python字典数据类型详解
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908116.html
Copyright © 2011-2022 走看看