Spring 对象注入 (直接使用bean标签)

1.写一个带bean标签的类Person.java

package cn.itsource._05_set;

import java.math.BigDecimal;
import java.util.*;

public class Person {

    // 简单属性【8种基本数据类型及其包装类型,String/BigDecimal】
    private Long id;
    private String name;
    private Boolean sex;
    private BigDecimal salary;

    // 对象属性
    private OtherBean otherBean;
    private String[] arrays;
    private List<String> list;
    private Set<String> set;


    //下面这个是重点
    private Properties props1;
    private Properties props2;
    private List<OtherBean> otherBeanList;
    private Set<OtherBean> otherBeanSet;
    private Map<String, Object> map;

    @Override
    public String toString() {
        System.out.println("map ===> " + map);
        System.out.println("otherBeanSet ===> " + otherBeanSet);
        System.out.println("otherBeanList ===> " + otherBeanList);
        System.out.println("props2 ===> " + props2);
        System.out.println("props1 ===> " + props1);
        System.out.println("set ===> " + set);
        System.out.println("list ===> " + list);
        System.out.println("arrays ===> " + Arrays.asList(arrays));
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", salary=" + salary +
                '}';
    }

    public OtherBean getOtherBean() {
        return otherBean;
    }

    public void setOtherBean(OtherBean otherBean) {
        this.otherBean = otherBean;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public List<OtherBean> getOtherBeanList() {
        return otherBeanList;
    }

    public void setOtherBeanList(List<OtherBean> otherBeanList) {
        this.otherBeanList = otherBeanList;
    }

    public Set<String> getSet() {
        return set;
    }

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

    public Set<OtherBean> getOtherBeanSet() {
        return otherBeanSet;
    }

    public void setOtherBeanSet(Set<OtherBean> otherBeanSet) {
        this.otherBeanSet = otherBeanSet;
    }

    public Map<String, Object> getMap() {
        return map;
    }

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

    public Properties getProps1() {
        return props1;
    }

    public void setProps1(Properties props1) {
        this.props1 = props1;
    }

    public Properties getProps2() {
        return props2;
    }

    public void setProps2(Properties props2) {
        this.props2 = props2;
    }

    public String[] getArrays() {
        return arrays;
    }

    public void setArrays(String[] arrays) {
        this.arrays = arrays;
    }

    public List<String> getList() {
        return list;
    }

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

2.在spring.xml中配置

 person类中有不同类型的字段,使用不同的方法注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       default-autowire="byName"
       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">


    <!--
        简单类型的属性值注入:
            采用property标签或者constructor-arg标签的value属性直接填值
        注入另一个bean对象:
            可以使用ref属性引用一个外部bean的id,或者使用内部bean配置
        注入一个简单类型的数组:
            使用array标签,内部使用value标签这样来填值
            也可以直接在value属性中写内容,以逗号隔开【不推荐】
        注入一个简单类型的List集合:
            使用list标签,内部使用value标签这样来填值
        注入一个简单类型的Set集合:
            使用set标签,内部使用value标签这样来填值,list标签和set标签唯一的区别就是set标签内部不允许数据重复
        注入一个Properties对象:
            ① 写props标签,内部写prop标签,每一个prop标签都表示一个键值对,键和值都必须是字符串类型
            ② 写value标签,内部直接将一个properties文件的文本内容直接写在value标签内部,不支持中文,支持注释内容
        注入一个List<OtherBean>类型的属性:
            在list标签内部使用内部bean写法:
                <list>
                    <bean class="cn.itsource._05_set.OtherBean"/>
                    <bean class="cn.itsource._05_set.OtherBean"/>
                    <bean class="cn.itsource._05_set.OtherBean"/>
                </list>
            在list标签内部使用外部bean写法:(bean属性填另一个bean标签的id)
                <list>
                    <ref bean="otherBean02"/>
                    <ref bean="otherBean03"/>
                    <ref bean="otherBean04"/>
                </list>
        注入一个Set<OtherBean>类型的属性与List<OtherBean>做法一致。
        注入一个Map<String,Object>类型的属性:
            内部写map标签,map标签内部写entry标签,每一个entry标签表示一个键值对
                key 以字符串类型作为键【最常用】
                key-ref 以其他bean类型作为键,填的是另一个bean标签的id【一般不常用】
                value 简单类型的值就用value属性
                value-ref 如果值是另一个bean对象,则用value-ref属性,填另一个bean标签的id
    -->
    <!--写了bean标签就是创建对象-->
    <!--使用set方法初始化对象-->
    <bean id="person" class="cn.itsource._05_set.Person" autowire="byType">
        <property name="id" value="123" />
        <property name="name" value="张三丰" />
        <property name="sex" value="true" />
        <property name="salary" value="5000" />

        <property name="otherBean" ref="otherBean01"/>
        <property name="arrays">
            <array>
                <value>张三丰</value>
                <value>张无忌</value>
                <value>张翠山</value>
                <value>张家辉</value>
            </array>
        </property>
        <!-- 不推荐使用这个方式,因为字符串中间万一出现逗号就有问题了 -->
        <!--<property name="arrays" value="张三丰,张无忌,张翠山,张家辉" />-->
        <property name="list">
            <list>
                <value>张三丰</value>
                <value>张无忌</value>
                <value>张翠山</value>
                <value>张家辉</value>
                <value>张家辉</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>张三丰</value>
                <value>张无忌</value>
                <value>张翠山</value>
                <value>张家辉</value>
                <value>张家辉</value>
            </set>
        </property>
        <property name="props1">
            <props>
                <prop key="name">张三丰</prop>
                <prop key="age">23</prop>
                <prop key="birthDay">2019-12-04</prop>
            </props>
        </property>
        <property name="props2">
            <value>
                #注释内容
                name=admin
                age=20
                birthDay=2019-12-04
            </value>
        </property>
        <property name="otherBeanList">
            <!--<list>
                <bean class="cn.itsource._05_set.OtherBean"/>
                <bean class="cn.itsource._05_set.OtherBean"/>
                <bean class="cn.itsource._05_set.OtherBean"/>
            </list>-->
            <list>
                <ref bean="otherBean02"/>
                <ref bean="otherBean03"/>
                <ref bean="otherBean04"/>
            </list>
        </property>
        <property name="otherBeanSet">
            <!--<set>
                <bean class="cn.itsource._05_set.OtherBean"/>
                <bean class="cn.itsource._05_set.OtherBean"/>
                <bean class="cn.itsource._05_set.OtherBean"/>
            </set>-->
            <set>
                <ref bean="otherBean02"/>
                <ref bean="otherBean03"/>
                <ref bean="otherBean04"/>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="aaa" value="张三丰"/>
                <entry key="bbb" value="235"/>
                <entry key="ccc" value-ref="otherBean01" />
            </map>
        </property>
    </bean>
    <bean id="otherBean01" class="cn.itsource._05_set.OtherBean"/>
    <bean id="otherBean02" class="cn.itsource._05_set.OtherBean"/>
    <bean id="otherBean03" class="cn.itsource._05_set.OtherBean"/>
    <bean id="otherBean04" class="cn.itsource._05_set.OtherBean"/>
</beans>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值