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;@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命名注入,和集合注入相关推荐
- Spring-学习笔记03【Spring的IOC和DI】
Java后端 学习路线 笔记汇总表[黑马程序员] Spring-学习笔记01[Spring框架简介][day01] Spring-学习笔记02[程序间耦合] Spring-学习笔记03[Spring的 ...
- java元婴期(18)----java进阶(spring(2)----DI(依赖注入)基于注解的IOC与DI配置properties)
1.依赖注入的概念 依赖注入:Dependency Injection.它是spring框架核心ioc的具体实现. 我们的程序在编写时,通过控制反转,把对象的创建交给了spring,但是代码中不可能出 ...
- 【Spring】IOC和DI
背景 private IAccountDao accountDao=new AccountDaoImpl(); 采用new的方式创建对象,找对象是主动的,APP和资源之间存在无法消除的联系,对于资源和 ...
- 【Spring】Spring 学习
目录 一.什么是 Spring 二.Spring 的特点 三.Spring核心概念 3.1 IOC(控制反转) 3.1.1 概念 3.1.2 为什么叫控制反转 3.1.3 依赖注入 3.1.4 IOC ...
- 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 ...
- SSM框架:Spring
SSM框架:Spring 文章目录 前言 一.Spring 1. 简介 2. 优点 3. 组成 4. 拓展 二.IOC理论推导 1. IOC原型引入 2. IOC本质(基本思想) 三.HelloSpr ...
- SSM Chapter 05 Spring 核心概念
SSM Chapter 05 Spring 核心概念 笔记 本章目标: 理解Spring IoC的原理 掌握Spring IoC的配置 理解Spring AOP的原理 掌握Spring AOP的配置 ...
- 互相引用 spring_巧夺天工,这样理解Spring的IOC、DI下来,真的很清晰了
前言 你可能会有如下问题: 想看Spring源码,但是不知道应当如何入手去看,对整个Bean的流程没有概念,碰到相关问题也没有头绪如何下手 看过几遍源码,没办法彻底理解,没什么感觉,没过一阵子又忘了本 ...
- JavaWeb学习之路——SSM框架之Spring(五)
前情提要请看JavaWeb学习之路--SSM框架之Spring(四) 整合Spring和Mybatis框架 1.在项目的 ...
- JavaWeb学习之路——SSM框架之Spring(四)
SSM框架学习-Spring01 1,.Spring介绍 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同 ...
最新文章
- 神策游戏解决方案:赋能游戏产业精品化研运升级
- TFS自定义开发中的反射应用
- 玩转Linux必备知识(四)
- 电子邮箱里面的服务器,搭建电子邮件服务器
- mysql怎么给表设置查询语句_MySQL查询语句简单操作示例
- 删文97篇!前UCLA教授竟是民科?不看好量子通信被禁言
- 技校毕业是什么学历_初中毕业上技校学什么专业好
- Objective-c 中 nil, Nil, NULL和NSNull的区别
- android中界面布局文件放在,android界面布局详解.doc
- 电路设计_RS485总线典型电路介绍
- 滴滴如何调度_滴滴智能调度浅析
- 颜值是第一生产力 - Windows Terminal
- javascript实现小米搜索框
- Opencv-python滤镜系列(3): 凹透镜滤镜效果实现
- 详解注意力机制和Transformer
- auto.js悬浮窗按钮的实际使用
- linux如何解压.z文件,linux文件解压缩命令(史上最全教程)
- 经典:uC/OS-II系统的学习教程之(2)
- Java——泛型和Io流
- 如何在Docker中运行H5S视频平台(h5stream)