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

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;@Overridepublic 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命名注入,和集合注入相关推荐

  1. Spring-学习笔记03【Spring的IOC和DI】

    Java后端 学习路线 笔记汇总表[黑马程序员] Spring-学习笔记01[Spring框架简介][day01] Spring-学习笔记02[程序间耦合] Spring-学习笔记03[Spring的 ...

  2. java元婴期(18)----java进阶(spring(2)----DI(依赖注入)基于注解的IOC与DI配置properties)

    1.依赖注入的概念 依赖注入:Dependency Injection.它是spring框架核心ioc的具体实现. 我们的程序在编写时,通过控制反转,把对象的创建交给了spring,但是代码中不可能出 ...

  3. 【Spring】IOC和DI

    背景 private IAccountDao accountDao=new AccountDaoImpl(); 采用new的方式创建对象,找对象是主动的,APP和资源之间存在无法消除的联系,对于资源和 ...

  4. 【Spring】Spring 学习

    目录 一.什么是 Spring 二.Spring 的特点 三.Spring核心概念 3.1 IOC(控制反转) 3.1.1 概念 3.1.2 为什么叫控制反转 3.1.3 依赖注入 3.1.4 IOC ...

  5. SSM—Spring框架,IOC理论推导,Hello Spring,IOC创建对象方式,Spring的配置,DI(依赖注入)

    文章目录 1.Spring 1.1.Spring简介(了解) 1.2.spring优点 1.3.组成(七大模块) 1.4.拓展 2.IOC理论推导 2.1.IOC本质 3.Hello Spring 4 ...

  6. SSM框架:Spring

    SSM框架:Spring 文章目录 前言 一.Spring 1. 简介 2. 优点 3. 组成 4. 拓展 二.IOC理论推导 1. IOC原型引入 2. IOC本质(基本思想) 三.HelloSpr ...

  7. SSM Chapter 05 Spring 核心概念

    SSM Chapter 05 Spring 核心概念 笔记 本章目标: 理解Spring IoC的原理 掌握Spring IoC的配置 理解Spring AOP的原理 掌握Spring AOP的配置 ...

  8. 互相引用 spring_巧夺天工,这样理解Spring的IOC、DI下来,真的很清晰了

    前言 你可能会有如下问题: 想看Spring源码,但是不知道应当如何入手去看,对整个Bean的流程没有概念,碰到相关问题也没有头绪如何下手 看过几遍源码,没办法彻底理解,没什么感觉,没过一阵子又忘了本 ...

  9. JavaWeb学习之路——SSM框架之Spring(五)

    前情提要请看JavaWeb学习之路--SSM框架之Spring(四)                                         整合Spring和Mybatis框架 1.在项目的 ...

  10. JavaWeb学习之路——SSM框架之Spring(四)

    SSM框架学习-Spring01 1,.Spring介绍 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同 ...

最新文章

  1. 超越PVT、Swin,南大开源高效Transformer:ResT​
  2. WORD 排版十技巧
  3. 网络化机房的绿色安全卫士——万联OMM网络化机房动力环境监控系统案例分析...
  4. Python之format格式化输出
  5. ufldl matlab 工具箱,matlab的Deep Learning的toolbox 中的SAE算法
  6. Linux部署Ant Design Pro项目及nginx部署
  7. linux top交叉编译_Linux 系统下ARM Linux交叉编译环境crosstool工具
  8. 在c语言中load,一道题理清Objective-C中的load和initialize
  9. Git学习总结(24)——彻底搞懂 Git-Rebase
  10. sql查询非ascii字符_SQL替换:如何在SQL Server中替换ASCII特殊字符
  11. sqlserver的for xml path和mysql的group_concat的区别
  12. JAVA与C#的区别
  13. 经典Flash MX 2004教程全集
  14. 重庆钢铁泛微oa系统服务器更新时间,泛微全新OA系统-协同办公系统
  15. imageJ下载链接
  16. android融云自定义通知,android融云消息免打扰
  17. isis和ospf比较
  18. Android Q 获取设备唯一ID(UDID\GUID\UUID\SSAID\GAID)
  19. html5 移动端手写签名,H5移动端项目实现手写签名功能 vue实现手写签名
  20. 解决mathtype中集合交并运算符号太矮的问题

热门文章

  1. MongoDb学习(四)--Repository
  2. 动产抵押物监控系统/金融抵押监控系统设计与实现
  3. sql语言的一大类 DML 数据的操纵语言
  4. AfxMessageBox详细使用说明
  5. 关于erlang的-run 的启动参数
  6. 团队开发——冲刺1.e
  7. MapReduce Design Patterns(6 、Job 链)(十二)
  8. 强连通分量[trajan]
  9. ESS And 迅雷5 让我不能上网
  10. WebSocket消息推送(实现进行聊天)和WebSocket简介