Spring的set注入法

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}}
         */
    }
}

上一篇:Spring全注解开发----Servlet 3.0


下一篇:源码解析: Spring RabbitMQ消费者