spring DI依赖注入

2021-04-14 12:29

阅读:545

标签:obb   ram   rri   pre   标签   依赖注入   span   oss   release   

1、官网

https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-dependencies

2、构造器注入(前面有)

3、Set方式注入(重点)

A、依赖 : bean对象 依赖 容器 创建

B、注入: bean对象的属性,由容器注入

https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#beans-factory-properties-detailed

案例

a、pojo

Address.java

package com.wt.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address=‘" + address + ‘\‘‘ +
                ‘}‘;
    }
}

Student.java

package com.wt.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set games;
    private String wife;
    private Properties info;

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + ‘\‘‘ +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife=‘" + wife + ‘\‘‘ +
                ", info=" + info +
                ‘}‘;
    }

    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 getHobbys() {
        return hobbys;
    }

    public void setHobbys(List hobbys) {
        this.hobbys = hobbys;
    }

    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 getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

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

b、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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    bean id="address" class="com.wt.pojo.Address">
        property name="address" value="徐州大庙李井村"/>
    bean>

    bean id="user" class="com.wt.pojo.Student" name="student">
        
        property name="name" value="吴通"/>
        
        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>
            list>
        property>

        property name="card">
            map>
                entry key="银行卡" value="7458694756870">entry>
                entry key="电话卡" value="13057122577">entry>
            map>
        property>

        property name="games">
            set>
                value>WOWvalue>
                value>鬼泣value>
            set>
        property>

        property name="wife">
            null/>
        property>

        property name="info">
            props>
                prop key="driver">driverprop>
                prop key="url">urlprop>
                prop key="port">3306prop>
                prop key="user">rootprop>
            props>
        property>
    bean>

beans>

c、测试

import com.wt.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {
    @Test
    public void getStudent(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student userInfo = (Student) context.getBean("student");
        System.out.println(userInfo.toString());
    }
}

 

4、其它方式注入

A、p命名空间

property

xml格式 引入p="***"

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

beans>

例子

   bean id="user" class="com.wt.pojo.User">
        property name="name" value="tom"/>
        property name="age" value="18"/>
    bean>
    bean id="user2" class="com.wt.pojo.User" p:name="loss" p:age="34"/>

两个bean标签的效果是一样的

B、c命名空间(有含参构成函数)

构造器注入

xml文件 添加 c="*****"

beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

beans>

例子

    bean id="user3" class="com.wt.pojo.User">
        constructor-arg name="name" value="gg"/>
        constructor-arg name="age" value="45"/>
    bean>

    bean id="user4" class="com.wt.pojo.User" c:name="死侍" c:age="100"/>

上述两种情况一致

 

spring DI依赖注入

标签:obb   ram   rri   pre   标签   依赖注入   span   oss   release   

原文地址:https://www.cnblogs.com/wt7018/p/13337300.html


评论


亲,登录后才可以留言!