Spring 04 - 对象取别名、import、各种类型的属性值依赖注入、命名空间(p,c)注入

1.对象取别名

1.1 testSpring 取多个别名 testSpring0,testSpring00,t00,testSpring000

    <alias name="testSpring" alias="testSpring0,testSpring00 t00;testSpring000"/>

1.2 testSpring 取别名 testSpring00

    <bean id="testSpring" class="TestSpring" name="testSpring 00;testSpring000">
        <property name="name" value="hello Spring"/>
    </bean>

2.合并多个配置文件: import

<import resource = "bean.xml" />
<import resource = "bean1.xml" />

3.DI 依赖注入

3.1 构造器(有参、无参)注入
例:

    <bean id="testSpring" class="TestSpring"/>

3.2 Set方式注入

( 需要属性的 set方法)

3.2.1 简单类型的属性值注入

    <bean id="student" class="com.qq.Student">
        <property name="name" value="张三"></property>
    </bean>

3.2.2 Map类型的属性值注入

        <property name="card">
            <map>
                <entry key="identity" value="001001234567908" />
                <entry key="bank" value="621226020123456" />
            </map>
        </property>

3.3.3 Set类型的属性值注入

        <property name="games">
            <set>
                <value>WOW</value>
                <value>LOL</value>
            </set>
        </property>

3.3.4 数组类型的属性值注入

        <property name="books">
            <array>
                <value> C++ </value>
                <value> Java </value>
                <value> Vue </value>
            </array>
        </property>

3.3.5 List类型的属性值注入

        <property name="hobbies">
            <list>
                <value>music</value>
                <value>movie</value>
                <value>badminton</value>
            </list>
        </property>

3.3.6 自定义对象类型的属性值注入

bean化对象

    <bean id="address" class="com.qq.Address">
        <property name= "addr" value="Melbourne"/>
    </bean>

ref引入

    <property name="address" ref="address"></property>

3.3.7 null注入

        <property name="graduate">
            <null/>
        </property>

3.3.8 Properties类型的属性值注入

        <property name="info">
            <props>
                <prop key="id">20200727</prop>
                <prop key="birth">20200726</prop>
            </props>
        </property>

3.3 命名空间注入
3.3.1 p命名空间注入属性值 (需要set方法)

xmlns:p="http://www.springframework.org/schema/p"

(1) 简单注入

    <!-- p命名空间,直接注入属性值-->
    <bean id="address1" class="com.qq.Address" p:addr="carlton" p:postcode="3053"/>

(2) bean对象注入

    <bean id="student" class="com.qq.Student" p:address-ref="address1"/>

3.3.2 c命名空间注入属性值(需要有参构造器)

xmlns:c="http://www.springframework.org/schema/c"
    <!-- c命名空间,直接注入属性值-->
    <bean id="address2" class="com.qq.Address" c:addr="CBD Melbourne" c:postcode="3053"/>

Student.class

package com.qq;

import java.util.*;

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

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

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

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

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

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

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

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

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String isGraduate() {
        return graduate;
    }

    public void setGraduate(String graduate) {
        this.graduate = graduate;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.getAddr() +
                ", card=" + card +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", games=" + games +
                ", info=" + info +
                ", gratudate=" + graduate +
                '}';
    }

}

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- student -->

    <bean id="address" class="com.qq.Address" scope="singleton">
        <property name= "addr" value="Melbourne"/>
    </bean>

    <bean id="student" class="com.qq.Student" p:address-ref="address">
        <property name="name" value="张三"></property>

<!--        <property name="address" ref="address1"></property>-->


        <property name="books">
            <array>
                <value> C++ </value>
                <value> Java </value>
                <value> Vue </value>
            </array>
        </property>

        <property name="hobbies">
            <list>
                <value>music</value>
                <value>movie</value>
                <value>badminton</value>
            </list>
        </property>

        <property name="games">
            <set>
                <value>WOW</value>
                <value>LOL</value>
            </set>
        </property>

        <property name="graduate">
            <null/>
        </property>

        <property name="info">
            <props>
                <prop key="id">20200727</prop>
                <prop key="birth">20200726</prop>
            </props>
        </property>

        <property name="card">
            <map>
                <entry key="identity" value="001001234567908" />
                <entry key="bank" value="621226020123456" />
            </map>
        </property>

    </bean>

    <!-- p命名空间,直接注入属性值-->
    <bean id="address1" class="com.qq.Address" p:addr="carlton" p:postcode="3053"/>

    <!-- c命名空间,直接注入属性值-->
    <bean id="address2" class="com.qq.Address" c:addr="CBD Melbourne" c:postcode="3053" />

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值