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

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="" value="孟六"></constructor-arg>
<constructor-arg index="" value=""></constructor-arg>
<constructor-arg index="" ref="car"></constructor-arg>
</bean>--> <!--p命名注入-->
<bean id="student" class="cn.dawn.day05diup.Student" p:name="孟小六" p:age="" 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></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. Java开发学习(七)----DI依赖注入之自动装配与集合注入

    一.自动配置 上一篇博客花了大量的时间把Spring的注入去学习了下,总结起来就两个字麻烦.麻烦在配置文件的编写配置上.那有更简单方式么?有,自动配置 1.1 依赖自动装配 IoC容器根据bean所依 ...

  2. SSM框架之Spring(2)IOC及依赖注入

    Spring(2)IOC及依赖注入 基于xml配置文件的实现 1.IOC (控制反转-Inversion Of Control) 控制反转(Inversion of Control,缩写为IoC),是 ...

  3. 04 Spring的依赖注入

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

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring DI(依赖注入)的实现方式属性注入和构造注入

    依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念. 当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的 ...

  5. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  6. Spring 04: IOC控制反转 + DI依赖注入

    Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ...

  7. SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇

    目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...

  8. java web后台开发SSM框架(Spring+SpringMVC+MyBaitis)搭建与优化

    一.ssm框架搭建 1.1创建项目 新建项目后规划好各层的包. 1.2导入包 搭建SSM框架所需包百度云链接:http://pan.baidu.com/s/1cvKjL0 1.3整合spring与my ...

  9. 转载百度百科上的强回复,关于spring的IOC和DI

    IoC与DI   首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命 ...

随机推荐

  1. 仿百度壁纸客户端(一)——主框架搭建,自定义Tab+ViewPager+Fragment

    仿百度壁纸客户端(一)--主框架搭建,自定义Tab+ViewPager+Fragment 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Fragment ...

  2. Xcode出现may cause a leak的解决

    比如如下代码: -(void)performSelector:(SEL)selector onNode:(CCNode *)node withObject:(id)object recursive:( ...

  3. Spring--ClassPathResource

    /* * 用一个给定的类加载器或者给定的类来加载资源 */ public class ClassPathResource extends AbstractFileResolvingResource { ...

  4. PS 滤镜——平面坐标变换到极坐标

    %%% orthogonal coordinate to polar coordinate %%% 平面坐标转极坐标 clc; clear all; close all; addpath('E:\Ph ...

  5. SharePoint 入门级介绍

    前言:接触SharePoint两年有余,从一开始的小白,变成现在的菜鸟,一路走来,学到很多,现在,想把自己知道的东西,写给大家,尤其是刚刚接触SharePoint的人们,做一个简单的参考.从一开始接触 ...

  6. ruby抓取web页面

    一种方法是Net::HTTP.new方法,返回resp码和实际的data: require 'net/http' h = Net::HTTP.new("www.baidu.com" ...

  7. iOS苹果自带UIMenuController

    一.UIMenuController认识 1.默认情况下,UITextView / UITextFiled / UIWebView 都有苹果自带的有UIMenuController功能 2.UITex ...

  8. 二叉树的序列化和反序列化(Java)

    请实现两个函数,分别用来序列化和反序列化二叉树 序列化就是将二叉树以字符串输出,反序列化:根据自己输出的字符串,构建二叉树. 这里先序遍历输出,且为了方便反序列化,各个节点","隔 ...

  9. j2EE经典面试题

    1. hibernate中离线查询去除重复项怎么加条件? dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 2. http协议及端口,sm ...

  10. java垃圾回收总结(2)

    java垃圾回收总结(2)   上一篇文章 介绍了jvm虚拟机运行时内存结构以及如何标识需要回收的对象,这一节主要讲解垃圾回收的基本算法. 基本上 jvm内存回收有三种 基本算法 标记-清除 标记清除 ...