码迷,mamicode.com
首页 > 编程语言 > 详细

Spring DI依赖注入

时间:2021-03-29 12:17:03      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:导入   src   音乐   指针   需要   string   location   格式   bcp   

构造器注入

即之前的学习

通过set注入(重点)

通过之前的手段,难以对类中复杂的类型去进行赋值

复杂类

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String NU;//空指针
    private Properties infe;

    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 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 Map<String, String> getCard() {
        return card;
    }

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

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

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

    public String getNU() {
        return NU;
    }

    public void setNU(String NU) {
        this.NU = NU;
    }

    public Properties getInfe() {
        return infe;
    }

    public void setInfe(Properties infe) {
        this.infe = infe;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘\‘‘ +
                ", address=" + address.getAddress() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", NU=‘" + NU + ‘\‘‘ +
                ", infe=" + infe +
                ‘}‘;
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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="Address">
            <property name="address" value="福建"/>
        </bean>
        <bean id="student" class="Student">
            <!--普通注入-->
            <property name="name" value="小明"/>
            <!--注入对象-->
            <property name="address" ref="address"/>
            <!--注入数组-->
            <property name="books">
                <array>
                    <value>书1</value>
                    <value>书2</value>
                    <value>书3</value>
                    <value>书4</value>
                </array>
            </property>
            <!--List-->
            <property name="hobbies">
                <list>
                    <value>听音乐</value>
                    <value>打游戏</value>
                </list>
            </property>
            <!--Map-->
            <property name="card">
                <map>
                    <entry key="信用卡" value="123"/>
                    <entry key="学生卡" value="456"/>
                </map>
            </property>
            <!--List-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>CS</value>
                </set>
            </property>
            <!--NULL-->
            <property name="NU">
                <null></null>
            </property>
            <property name="infe">
                <props>
                    <prop key="username"> ming</prop>
                    <prop key="password"> 123456</prop>
                </props>
            </property>
        </bean>
</beans>

其他方式注入

可以通过命名空间去自定义标签。
有P标签和C标签
简单例子

<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"
    <!--需要额外导入上面这句语句才能用P标签-->
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close"
    <!--对属性的赋值格式变成如下情况-->
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/mydb"
        p:username="root"
        p:password="misterkaoli"/>

</beans>

c命名空间

官方文档演示:
技术图片

Spring DI依赖注入

标签:导入   src   音乐   指针   需要   string   location   格式   bcp   

原文地址:https://www.cnblogs.com/OfflineBoy/p/14585007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!