Spring的DI(Dependency Injection)
写在之前,作为正在学习的程序员,对于Spring理解比较差,给两个简单的定义大家看一下。
控制反转(Inversion of Control),是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心,beans。
DI依赖注入:说的是创建对象实例时,为这个对象注入属性值或其它对象实例,侧重于实现。
今天的博客主要写的是DI,所以我们看的是实现,而不是思想。
首相我们先来看最简单的set方法注入
我们准备一个实体类就可以做第一个例子,
publi class Users(){
private String name; private Integer age;
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <context:component-scan base-package="cn.curry"></context:component-scan>
-->
<bean id="users" class="cn.curry.spring.Users">
<property name="name" value="呵呵"></property>
</bean>
上面是我们的部分配置文件。
最后我们看简单的测试
@Test
public void test01(){
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(resource);
Users users=(Users)bf.getBean("users");
Users userss=(Users)bf.getBean("users");
System.out.println(users==userss);
System.out.println(users.getName());
}
然后我们再看域属性注入和P命名空间注入
这时我们会添加第二个实体类,并且在Users类里加一个构造
public Users() {
System.out.println("Object Init");
} public Users(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
}
public class Car {private String color; public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
配置文件
<bean id="us2" class="cn.curry.spring.Users">
<constructor-arg index="0" value="花花"></constructor-arg>
<constructor-arg index="1" value="12"></constructor-arg>
<constructor-arg index="2" ref="car"></constructor-arg>
</bean> <bean id="usersP" class="cn.curry.spring.Users" p:name="花花" p:age="12" p:car-ref="car"></bean>
看测试
@Test
public void test02(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("us2");
System.out.println(users.getCar().getColor()+" "+users.getName()); }
@Test
public void test03(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usersP");
System.out.println(users.getCar().getColor()+" "+users.getName());
}
然后看集合属性注入
先修改Users,添加集合属性
private List<Car> list; private Set<Car> set; private Map<Integer,Car> map;
在看配置文件
<bean id="uslist" class="cn.curry.spring.Users">
<property name="list">
<list>
<ref bean="car"></ref>
</list>
</property>
</bean> <bean id="usset" class="cn.curry.spring.Users">
<property name="set">
<set>
<ref bean="car"></ref>
</set>
</property>
</bean>
<bean id="usmap" class="cn.curry.spring.Users">
<property name="map">
<map>
<entry key="1" value-ref="car"></entry>
</map>
</property>
</bean>
在看测试
@Test
public void test04(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("uslist");
List<Car> list = users.getList();
for (Car item : list) {
System.out.println(item.getColor());
}
}
@Test
public void test05(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usset");
Set<Car> set = users.getSet();
for (Car item : set) {
System.out.println(item.getColor());
}
}
@Test
public void test06(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usmap");
Map<Integer, Car> map = users.getMap();
for (Integer item : map.keySet()) {
System.out.println(map.get(item).getColor());
}
}
最后我们看一下注解这时配置只需一句话
<context:component-scan base-package="cn.curry"></context:component-scan>
然后看我们的实体类
package cn.curry.spring; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set; /**
* Created by Curry on 2017/3/25.
*/
@Component("usss")
public class Users {
@Value("赫尔")
private String name;
@Value("18")
private Integer age;
//@Resource(name="car")
@Autowired
@Qualifier("car")
private Car car;
private List<Car> list; private Set<Car> set; private Map<Integer,Car> map; public Users() {
System.out.println("Object Init");
} public Users(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
} 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;
} public List<Car> getList() {
return list;
} public void setList(List<Car> list) {
this.list = list;
} public Set<Car> getSet() {
return set;
} public void setSet(Set<Car> set) {
this.set = set;
} public Map<Integer, Car> getMap() {
return map;
} public void setMap(Map<Integer, Car> map) {
this.map = map;
}
}
package cn.curry.spring; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; /**
* Created by Curry on 2017/3/25.
*/
@Component("car")
//@Service
//@Controller
//@Repository
public class Car {
@Value("黄色")
private String color; public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
}
}
最后一个测试
package cn.curry.test; import cn.curry.spring.Car;
import cn.curry.spring.Users;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import java.util.List;
import java.util.Map;
import java.util.Set; /**
* Created by Curry on 2017/3/25.
*/
public class MyTest {
@Test
public void test01(){
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(resource);
Users users=(Users)bf.getBean("users");
Users userss=(Users)bf.getBean("users");
System.out.println(users==userss);
System.out.println(users.getName());
}
@Test
public void test02(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("us2");
System.out.println(users.getCar().getColor()+" "+users.getName()); }
@Test
public void test03(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usersP");
System.out.println(users.getCar().getColor()+" "+users.getName());
}
@Test
public void test04(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("uslist");
List<Car> list = users.getList();
for (Car item : list) {
System.out.println(item.getColor());
}
}
@Test
public void test05(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usset");
Set<Car> set = users.getSet();
for (Car item : set) {
System.out.println(item.getColor());
}
}
@Test
public void test06(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usmap");
Map<Integer, Car> map = users.getMap();
for (Integer item : map.keySet()) {
System.out.println(map.get(item).getColor());
}
}
@Test
public void test07(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Users users=(Users) context.getBean("usss");
System.out.println(users.getName()+" "+users.getCar().getColor());
}
}
Spring的DI(Dependency Injection)的更多相关文章
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- 理解依赖注入(DI - Dependency Injection)
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
- Spring系列(二):Spring IoC/DI的理解
这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...
- 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)
一. mybatis的高级映射 1 单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...
- Spring IOC/DI和AOP原理
一 IOC/DI 1. 概念机原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...
- What is dependency injection and when/why should or shouldn't it be used?
参考:https://stackoverflow.com/questions/130794/what-is-dependency-injection 原文:https://www.jamesshore ...
- Spring IOC/DI和AOP原理(转发:https://www.cnblogs.com/techroad4ca/p/5770073.html)
一 IOC/DI 1. 概念及原理 IOC: Inversion of Control(控制反转)是一种设计思想,就是容器控制应用程序所需要外部资源的创建和管理,然后将其反转给应用程序.对象及其依赖对 ...
- spring IOC(DI)和AOP
软件152谭智馗 IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. DI—Dependency Injecti ...
随机推荐
- APNS消息推送实现
转自:http://blog.csdn.net/biaobiaoqi/article/details/8058503 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1 ...
- iView之DatePicker的datetimerange校验
使用DatePicker的type是datetimerange时,处理开始--结束的持续时间校验如下.遇到的问题:时间弹出校验提示,但是程序还是会继续往下走,所以调完校验后,再做判断开始时间是否为tr ...
- 关于REST的一些想法
REST and RESTful 最近入手了REST,谈谈自己的体会. 所谓REST, 我觉得是一种网址的设计风格.过去我们用Struts 或Spring MVC 时从来没有考虑过URL的设计风格.所 ...
- Auto Layout Guide----(一)-----Understanding Auto Layout
Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the ...
- UVaLive 3530 Martian Mining (简单DP)
题意:给定一个n*m的网格,每个格子里有A矿和B矿数量,A必须由右向左运,B只能从下向上运,中间不能间断,问最大总数量. 析:一个简单DP,dp[i][j] 表示 从 (0, 0) 到 (i, j) ...
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...
- HDU - 6081 2017百度之星资格赛 度度熊的王国战略
度度熊的王国战略 Accepts: 644 Submissions: 5880 Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 3 ...
- 【异步编程】Part1:await&async语法糖让异步编程如鱼得水
前导 Asynchronous programming Model(APM)异步编程模型以BeginMethod(...) 和 EndMethod(...)结对出现. IAsyncResult Beg ...
- hihocoder1475 数组分拆【DP+前缀和优化】
思路: DP[ i ] 代表以 i 结尾的方案数. dp[i] += sum[i] - sum[j - 1] != 0 ? dp[j] : 0 ; 对于100%的数据,满足1<=N<=10 ...
- js转义字符
\" \/ \" 例: span = "<span onclick=\"xadmin.open('编辑','\/junyi\/member\/up ...