Spring之IoC----常见类型数据的注入

6 篇文章 0 订阅
4 篇文章 0 订阅

    Spring框架中的String,int等基本类型、List、Map、Properties等几种常见的类型数据的注入方法,以下举例说明:

1.首先编写实体类Person,代码如下:

package com.jeason.entity;

public class Person {
    private String name;
    private int age;
    private String gender; //性别
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
}

2.实体类Injection,代码如下:
public class Injection {
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Properties;
    import java.util.Set;


    private String name;
    private int password;
    private List<String> hbms;  //Set集合的注入方式与List集合一样
    private Map<String,Object> maps; //Map集合的注入
    private Properties props;
   
    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    public void setHbms(List<String> hbms) {
        this.hbms = hbms;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public void setProps(Properties props) {
        this.props = props;
    }
    
    //show方法用于遍历输出各个属性值
    public void show(){
        System.out.println("name:"+name);
        System.out.println("password:"+password);
        System.out.println("***hbms文件列表如下***");
        for(String s: hbms){
            System.out.println(s);
        }
        
        System.out.println("***遍历maps集合***");
        Set<Entry<String, Object>> ens = maps.entrySet();  //Entry是Map集合中的一个内部接口
        for(Entry en:ens){
            System.out.println(en.getKey()+":"+en.getValue());
        }
        
        System.out.println("***props参数如下***");
        Set keys = props.keySet();
        for(Object k:keys){
            System.out.println(k.toString()+": "+props.getProperty(k.toString()));
        }

    }

   
}
3.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="personBean" scope="prototype"
         class="com.jeason.entity.Person">
    </bean>

    <bean id="injection" class="com.jeason.entity.Injection">
        <property name="name" value="jeason" />
        <property name="password" value="123456" />
        
        <!-- List集合类型数据的注入 -->
        <property name="hbms">
            <list>
                <!-- 如果list里面为bean对象应该使用下面标签元素 -->
                <!-- <ref bean="" /> -->
                <value>/com/jeason/entity/cost.hbm.xml</value>
                <value>/com/jeason/entity/admin.hbm.xml</value>
                <value>/com/jeason/entity/account.hbm.xml</value>
                <value>/com/jeason/entity/role.hbm.xml</value>
            </list>
        </property>
        <!-- 由于Set集合注入方式与List集合一样,所以在此没有举例 -->
        <!-- Map集合数据类型的注入 -->
        <property name="maps">
            <map>
                <entry key="key1" value="China" />
                <entry key="key2" value-ref="personBean" />
                <entry key="key3">
                    <bean class="com.jeason.entity.Person">
                        <property name="name" value="JeasonWang" />
                        <property name="age" value="22" />
                        <property name="gender" value="female" />
                    </bean>
                </entry>
            </map>
        </property>
        
        <!-- Properties类型数据的注入 -->
        <property name="props">
            <props>
                <prop key="driver">oracle.jdbc.OracleDriver</prop>
                <prop key="url">jdbc:oracle:thin:@localhost:1521:xx</prop>
                <prop key="user">tiger</prop>
                <prop key="password">scott</prop>
            </props>
        </property>
    </bean>
</beans>

4.测试类TestInjection代码如下:

public class TestInjection {
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.jeason.entity.Injection;


    public static void main(String[] args){
        String cfg = "/applicationContext.xml";
        ApplicationContext atx = new ClassPathXmlApplicationContext(cfg);
        Injection ij = (Injection)atx.getBean("injection");
        ij.show();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值