• 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

  • 相关阅读:
    我的第一个博客在博客园安家了,哈哈~欢迎大家光临
    JS未设置对象问题解决方案
    关于Oracle的优化
    USACO 1.2 Milking Cows
    C语言的文件操作 freopen
    USACO 1.2 Palindromic Squares
    USACO 1.1 Broken Necklace
    ACM征程再次起航!
    USACO 1.3 Barn Repair
    USACO 1.2 Transformations
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908116.html
Copyright © 2020-2023  润新知