Spring DI依赖注入

2021-06-07 12:02

阅读:560

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

构造器注入

即之前的学习

通过set注入(重点)

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

复杂类

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbies;
    private Map card;
    private Set 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 getHobbies() {
        return hobbies;
    }

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

    public Map getCard() {
        return card;
    }

    public void setCard(Map card) {
        this.card = card;
    }

    public Set getGames() {
        return games;
    }

    public void setGames(Set 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 +
                ‘}‘;
    }
}

配置文件:

书1书2书3书4听音乐打游戏LOLCS ming 123456

其他方式注入

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


    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">

    
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/mydb"
        p:username="root"
        p:password="misterkaoli"/>

c命名空间

官方文档演示:
技术图片

Spring DI依赖注入

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

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


评论


亲,登录后才可以留言!