ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring的set注入法

2022-02-20 20:58:17  阅读:135  来源: 互联网

标签:set String Spring private tl address public 注入


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

标签:set,String,Spring,private,tl,address,public,注入
来源: https://blog.csdn.net/weixin_43716907/article/details/123035677

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有