Spring依赖属性注入属性设置

1.对象属性设置

String、list、set、map、properties
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="hello" class="com.spring.test.Hello">
        <property name="username">
            <value>wayne</value>
        </property>
        <property name="list">
            <list>
                <value>one</value>
                <value>two</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>one</value>
                <value>two</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry>
                    <key>
                        <value>title</value>
                    </key>
                    <value>依赖注入</value>
                </entry>
                <entry>
                    <key>
                        <value>id</value>
                    </key>
                    <value>1</value>
                </entry>
            </map>
        </property>

        <property name="props">
            <props>
                <prop key="gmail">wxiong6@gmail.com</prop>
                <prop key="qq">wxiong6@qq.com</prop>
            </props>
        </property>

        <constructor-arg value="1" index="0"></constructor-arg>
        <constructor-arg value="hello" index="1"></constructor-arg>
    </bean>
</beans>

2.java对象

package com.spring.test;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Hello {
    private String username;
    private List<?> list;
    private Set set;
    public Set getSet() {
        return set;
    }

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

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }

    private Map map;
    private Properties props;
    public List<?> getList() {
        return list;
    }

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

    public Hello(int index, String name) {
        System.out.println("index"+index+";name:"+name+";");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void doHello(){
        System.out.println("执行doHello()");
    }
}

3.对象调用

对这几种类型的调用方法

package com.spring.test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class HelloTest {    
    @Test
    public void testdoHello(){
        String [] str = {"applicationContext.xml","applicationContext-list.xml"};
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(str);
        Hello hello = (Hello) applicationContext.getBean("hello");
        hello.doHello();
        System.out.println(hello.getUsername());

        //list 遍历
        System.out.println("list...");
        List<?> list  = hello.getList();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        //set 遍历
        System.out.println("set....");
        Set<?> set = hello.getSet();
        Iterator<?> it = set.iterator();
        while (it.hasNext()) {
            String str1 = (String) it.next();
            System.out.println(str1);  

        }

        //map
        System.out.println("map......");
        Map<?, ?> map = hello.getMap();
        Set<?> keys = map.keySet();
        Iterator<?> mit = keys.iterator();
        while (mit.hasNext()) {
            String key = (String) mit.next();
            String value = (String) map.get(key);
            System.out.println("key:"+key+",value:"+value);
        }

        System.out.println("properties...");
        Properties props = hello.getProps();
        System.out.println(props.getProperty("qq"));
        System.out.println(props.getProperty("gmail"));
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值