SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入

Posted 晨曦Dawn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入相关的知识,希望对你有一定的参考价值。

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

 

DI和IOC相比,DI更偏向于实现

 

DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入

    构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错

    P命名则有注意的是那个头文件 xmlns  xsi需要你去配置一道,我下面有,你直接copy就可以

 

    在实体类中(有俩个实体类,我做了关联关系)

 

package cn.dawn.day05diup;

/**
 * Created by Dawn on 2018/3/5.
 */
public class Car {
    private String color;
    private String type;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}







package cn.dawn.day05diup;

/**
 * Created by Dawn on 2018/3/5.
 */
//student类
public class Student {
    private String name;
    private Integer age;
    private Car car;

    //带参构造
    public Student(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    //无参构造
    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

 

    在大配置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" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

    <bean id="car" class="cn.dawn.day05diup.Car">
        <property name="color" value="黑色"></property>
        <property name="type" value="奥迪"></property>
    </bean>
    <!--di构造注入-->
    <!--<bean id="student" class="cn.dawn.day05diup.Student">
        <constructor-arg index="0" value="孟六"></constructor-arg>
        <constructor-arg index="1" value="20"></constructor-arg>
        <constructor-arg index="2" ref="car"></constructor-arg>
    </bean>-->

    <!--p命名注入-->
    <bean id="student" class="cn.dawn.day05diup.Student" p:name="孟小六" p:age="8" p:car-ref="car"></bean>

</beans>

 

    没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index

    单测方法:

 

    @Test
    /*diP命名注入*/
    public void t02(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+""+student.getCar().getType());
    }



    @Test
    /*di构造注入*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+""+student.getCar().getType());
    }

 

 

集合注入:

    数组,List,Set,Map,Properties

  实体类

 

package cn.dawn.day05diup;

import java.util.*;

/**
 * Created by Dawn on 2018/3/5.
 */
public class MyCollection {
    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties properties;

    @Override
    public String toString() {
        return "MyCollection{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                };
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

 

  大配置中的bean节点

 

<!--di的集合注入-->
    <bean id="mycollection" class="cn.dawn.day05diup.MyCollection">
        <!--数组注入-->
        <property name="array">
            <array>
                <value>孟六</value>
                <value>孟六十六</value>
                <value>孟六百六十六</value>
            </array>
        </property>
        <!--list集合注入-->
        <property name="list">
            <list>
                <value>奥迪</value>
                <value>奥小迪</value>
                <value>奥迪迪</value>
            </list>
        </property>
        <!--set集合注入-->
        <property name="set">
            <set>
                <value>set1</value>
                <value>set2</value>
                <value>set3</value>
            </set>
        </property>
        <!--map集合注入-->
        <property name="map">
            <map>
                <entry key="姓名">
                    <value>孟五</value>
                </entry>
                <entry key="年龄">
                    <value>555</value>
                </entry>
            </map>
        </property>
        <!--properties-->
        <property name="properties">
            <props>
                <prop key="key1">v1</prop>
                <prop key="key2">v2</prop>
                <prop key="key3">v3</prop>
            </props>
        </property>
    </bean>

 

    单测

 

    @Test
    /*di集合注入*/
    public void t03(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
        MyCollection mycollection = (MyCollection) context.getBean("mycollection");
        System.out.println(mycollection);
    }

 

    由于重写了toString,可以直接一览无余

 

 

 

 

 

 

 

以上是关于SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入的主要内容,如果未能解决你的问题,请参考以下文章

Spring基础:XML方式实现DI

Spring万字详解DI相关内容,一文掌握DI配置与使用

Spring依赖注入 DI

spring ioc Di

Spring DI注入技术

Spring中IOC与DI的的区别