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

spring DI依赖注入

时间:2020-07-18 22:25:29      阅读:84      评论:0      收藏:0      [点我收藏+]

标签: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<String> hobbys;
    private Map<String, String> card;
    private Set<String> 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<String> getHobbys() {
        return hobbys;
    }

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

    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 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>
<!--        list-->
        <property name="hobbys">
            <list>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>
<!--        map-->
        <property name="card">
            <map>
                <entry key="银行卡" value="7458694756870"></entry>
                <entry key="电话卡" value="13057122577"></entry>
            </map>
        </property>
<!--        set-->
        <property name="games">
            <set>
                <value>WOW</value>
                <value>鬼泣</value>
            </set>
        </property>
<!--        null-->
        <property name="wife">
            <null/>
        </property>
<!--        Properties-->
        <property name="info">
            <props>
                <prop key="driver">driver</prop>
                <prop key="url">url</prop>
                <prop key="port">3306</prop>
                <prop key="user">root</prop>
            </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

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