1. 程式人生 > 實用技巧 >Spring基礎,DI(重點:依賴注入) 構造方式注入,set方式注入,c命名和p名稱空間注入

Spring基礎,DI(重點:依賴注入) 構造方式注入,set方式注入,c命名和p名稱空間注入

1,構造器注入

    <bean id="user" class="com.king.pojo.User">
        <!--無參構造建立物件-->
        <property name="name" value="king"/>
        <!--有參構造建立物件,index是下標0為第一個位置,value是賦的值-->
        <constructor-arg index="0" value="龍神"/>
    </bean>

2,Set方式注入

依賴注入:本質,set注入

  依賴:bean物件的建立依賴與容器

  注入:bean物件中的所有屬性,由容器來注入

【環境搭建】

  複雜型別

package com.king.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 + '\'' + '}'; } }

  

真實測試物件

public class Student {
    private String name;
    private Address address;
    private String[] book;
    private List<String> hobby;
    private Map<String,String> card;
    private
Set<String> games; private String wife; private Properties info;

bean注入資訊

<?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中註冊一個類,相當於建立一個物件-->
    <bean id="address" class="com.king.pojo.Address">
        <property name="address" value="哈爾濱"/>
    </bean>

    <bean id="student" class="com.king.pojo.Student">
        <!--第一種普通值注入-->
        <property name="name" value="king"/>

        <!--第二種,bean注入,ref:註冊(建立)之後就可以引用了-->
        <property name="address" ref="address"/>


        <!--陣列注入,ref-->
        <property name="book">
            <array>
                <value>紅樓夢</value>
                <value>西遊記</value>
                <value>水滸傳</value>
                <value>三國演義</value>
            </array>
        </property>

        <!--list-->
        <property name="hobby">
            <list>
                <value>跑步</value>
                <value>打籃球</value>
                <value>看電影</value>
            </list>
        </property>

        <!--map-->
        <property name="card">
            <map>
                <entry key="身份證" value="111111222222223333"/>
                <entry key="銀行卡" value="1111112222222233334444"/>
            </map>
        </property>

        <!--set-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>bob</value>
                <value>coc</value>
            </set>
        </property>

        <!--null-->
        <property name="wife">
            <null/>
        </property>

        <!--Properites-->
        <property name="info">
            <props>
                <prop key="driver">11111</prop>
                <prop key="url">0000</prop>
                <prop key="username">root</prop>
                <prop key="pwd">123456</prop>
            </props>
        </property>

    </bean>

</beans>

單元測試

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

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

效果

3,拓展方式注入

p 命名 空間注入(本質,傳的是property(傳屬性),set方式注入)

c 命名 空間注入(本質,構造器注入,construct-args)

注意:p,c名稱空間不能直接使用,需要加入相應的xml約束

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

xml

<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">

    <!--p名稱空間注入,可以直接注入屬性的值(property)-->
    <bean id="user" class="com.king.pojo.User" p:name="king" p:age="18"/>

    <!--c名稱空間注入,本質,構造器器注入construct-args-->
    <bean id="user2" class="com.king.pojo.User" c:age="18" c:name="kuang"/>


</beans>

單元測試

     @Test
    public void test2(){
         ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
         //體現反射機制,通過物件名,獲得這個類的屬性和物件
         User user = context.getBean("user",User.class);
         System.out.println(user.toString());
     }