基本知识—依赖注入(Dependency Injection)

  1. 依赖注入
    依赖:值bean对象的创建依赖于容器。
    注入:Bean对象依赖的资源由容器来设置和装配。

2.Spirng 注入 – 构造注入
3.Spirng 注入 – setter 注入
要求被注入的属性必须有set方法。set 的方法由 set+首字符大写构成。

3.1 常量注入
3.2 Bean注入
3.3 数组注入
3.4 List注入
3.5 Map注入
3.6 Set注入
3.7 Properties注入

<bean id="student" class="com.duoduo.Student">
        <!-- 常量注入 -->
        <property name="name" value="constant test"/>
        <!-- Bean 注入 -->
        <property name="address" ref="address"/>
        <!-- 数组注入 -->
        <property name="books">
            <array>
                <value>yuwen</value>
                <value>shuxue</value>
                <value>yingyu</value>
            </array>
        </property>
        <!-- List 注入 -->
        <property name="hobbies">
            <list>
                <value>basketball</value>
                <value>football</value>
            </list>
        </property>

        <!-- Map 注入 -->
        <property name="cards">
            <map>
                <entry key="ICBC" value="1234567890"/>
                <entry key="CMBC" value="12121212121"/>
            </map>
        </property>

        <!-- Set 注入 -->
        <property name="games">
            <set>
                <value>dota</value>
                <value>QQ game</value>
            </set>
        </property>

        <!-- Properties 注入 -->
        <property name="properties">
            <props>
                <prop key="no">2012011111</prop>
                <prop key="name">name</prop>
                <prop key="aget">20</prop>
            </props>
        </property>

    </bean>

    <bean id="address" class="com.duoduo.Address">
        <property name="addressDetail" value="student address information"/>
    </bean>

实体类

package com.duoduo;

import java.util.*;

/**
 * @author Duoduo
 * @version 1.0
 * @date 2017/5/13 14:13
 */
public class Student {
    private String name;
    private Address address;
    private String books[];
    private List<String> hobbies;
    private Map<String,String> cards;
    private Set<String> games;
    private Properties properties;


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", cards=" + cards +
                ", games=" + games +
                ", properties=" + properties +
                '}';
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Map<String, String> getCards() {
        return cards;
    }

    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

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


}

测试函数

@Test
    public void testSetName() throws Exception {

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        Student  student = (Student)context.getBean("student");
        System.out.println(student.toString());

    }

测试结果

Student{name='constant test', address=Address{addressDetail='student address information'}, books=[yuwen, shuxue, yingyu], hobbies=[basketball, football], cards={ICBC=1234567890, CMBC=12121212121}, games=[dota, QQ game], properties={name=name, no=2012011111, aget=20}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值