Spring的set注入法

set注入法是什么

在实体类中加入set方法,这个set方法名需要使用默认的,即驼峰命名法;否则Spring找不到方法,从而注入失败。

public void setAdd(String address) {
        this.add = address;
    }

各种类型属性的注入

配置文件

<?xml version="1.0" encoding="UTF8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.tl.pojo.Address">
        <property name="add" value="北京"/>
    </bean>
    <bean id="stu" class="com.tl.pojo.Student">
        <property name="name" value="tl"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>
        <property name="hobbys">
            <list>
                <value>游戏</value>
                <value>音乐</value>
                <value>运动</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="中国银行" value="413565656"/>
                <entry key="农业银行" value="565656566"/>
            </map>
        </property>
        <property name="games">
            <set>
                <value>只狼</value>
                <value>lol</value>
            </set>
        </property>
        <property name="girfriend">
            <null/>
        </property>
        <property name="info">
            <props>
                <prop key="学号">20202020</prop>
                <prop key="姓名">jack-tl</prop>
            </props>
        </property>
    </bean>

</beans>

实体类

这里的set方法太多不方便展示;
Student

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String girfriend;
    private Properties info;
}

Address

package com.tl.pojo;

/**
 * @author tl
 */
public class Address {
    private String add;

    public void setAdd(String address) {
        this.add = address;
    }

    @Override
    public String toString() {
        return  add;
    }
}

测试

import com.tl.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author tl
 */
public class Test {
    @org.junit.Test
    public void test1(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
        Student stu = (Student) context.getBean("stu");
        System.out.println(stu.toString());
        /*输出结果:
            Student{name='tl',
            address=北京,
            books=[红楼梦, 三国演义, 水浒传, 西游记],
            hobbys=[游戏, 音乐, 运动],
            card={中国银行=413565656, 农业银行=565656566},
            games=[只狼, lol],
            girfriend='null',
            info={学号=20202020, 姓名=jack-tl}}
         */
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值