Spring点滴七:Spring中依赖注入(Dependency Injection:DI)
Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Injection(基于Setter方法依赖注入)
一、Contructor-based Dependency Injection(基于构造方法注入)
在bean标签中通过使用<constructor-arg />标签来实现
spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="com.test.spring.A">
<!-- 引用其它bean两种不同实现-->
<constructor-arg>
<ref bean="b"/>
</constructor-arg>
<constructor-arg ref="c"/>
<constructor-arg name="name" value="张三"/>
</bean>
<bean id="b" class="com.test.spring.B">
</bean>
<bean id="c" class="com.test.spring.C">
</bean>
</beans>
Java 类:
package com.test.spring;
public class A {
private B b;
private C c;
private String name;
public A(B b, C c, String name) {
this.b = b;
this.c = c;
this.name = name;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "A [b=" + b + ", c=" + c + ", name=" + name + "]";
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
package com.test.spring;
public class B {
}
-------------------------------------------------------------------------------------------------------------------------------------------------
package com.test.spring;
public class C {
}
测试:
package com.test.spring; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
A a=applicationcontext.getBean(A.class);
System.out.println(a);
}
}
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 14:02:53 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 14:02:53 CST 2017]; root of context hierarchy
2017-03-19 14:02:53 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
A [b=com.test.spring.B@4899c2aa, c=com.test.spring.C@66bb4c22, name=张三]
Spring 官网API提供够了使用静态工厂方法来实现上述功能:
Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="a" class="com.test.spring.A" factory-method="getInstence">
<constructor-arg>
<ref bean="b"/>
</constructor-arg>
<constructor-arg ref="c"/>
<constructor-arg name="name" value="张三"/>
</bean>
<bean id="b" class="com.test.spring.B">
</bean>
<bean id="c" class="com.test.spring.C">
</bean>
</beans>
Java类:
package com.test.spring;
public class A {
private B b;
private C c;
private String name;
/**
* 创建一个私有的构造方法
* @param b
* @param c
* @param name
*/
private A(B b, C c, String name) {
this.b = b;
this.c = c;
this.name = name;
}
public static A getInstence(B b,C c,String name){
System.out.println("调用了静态的工厂方法");
A a = new A(b,c,name);
return a;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "A [b=" + b + ", c=" + c + ", name=" + name + "]";
}
}
--------------------------------------------------------------------------------
B.java C.java(略)
测试:
package com.test.spring; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
A a=applicationcontext.getBean(A.class);
A a2=applicationcontext.getBean(A.class);
System.out.println(a==a2);//发现A的实例对象是单例的
System.out.println(a);
System.out.println(a2);
}
}
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 14:22:31 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17ad352e: startup date [Sun Mar 19 14:22:31 CST 2017]; root of context hierarchy
2017-03-19 14:22:31 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
调用了静态的工厂方法
》》》Spring ApplicationContext容器初始化完毕了......
true
A [b=com.test.spring.B@176730bb, c=com.test.spring.C@77b050fd, name=张三]
A [b=com.test.spring.B@176730bb, c=com.test.spring.C@77b050fd, name=张三]
2.Setter-based Dependency Injection(基于Setter方式依赖注入):该注入方式是经常使用的
spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a" class="com.test.spring.A">
<property name="b" ref="b"/>
<property name="c" ref="c"/>
<property name="name" value="张三"/>
</bean>
<bean id="b" class="com.test.spring.B">
</bean>
<bean id="c" class="com.test.spring.C">
</bean>
</beans>
Java类:
package com.test.spring;
public class A {
private B b;
private C c;
private String name;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "A [b=" + b + ", c=" + c + ", name=" + name + "]";
}
}
测试:
package com.test.spring; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
//BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
//applicationcontext.close();
//applicationcontext.registerShutdownHook();
A a=applicationcontext.getBean(A.class);
System.out.println(a);
}
}
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 14:34:20 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17ad352e: startup date [Sun Mar 19 14:34:20 CST 2017]; root of context hierarchy
2017-03-19 14:34:20 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
A [b=com.test.spring.B@49daafb9, c=com.test.spring.C@3446c090, name=张三]
这两种注入方式在Spring配置文件中可以一起使用
Spring点滴七:Spring中依赖注入(Dependency Injection:DI)的更多相关文章
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...
- 简明依赖注入(Dependency Injection)
前言 这是因特奈特上面不知道第几万篇讲依赖注入(Dependency Injection)的文章,但是说明白的却寥寥无几,这篇文章尝试控制字数同时不做大多数. 首先,依赖注入的是一件很简单的事情. 为 ...
- 14.AutoMapper 之依赖注入(Dependency Injection)
https://www.jianshu.com/p/f66447282780 依赖注入(Dependency Injection) AutoMapper支持使用静态服务定位构建自定义值解析器和自定 ...
- 依赖注入 | Dependency Injection
原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...
- Spring之对象依赖关系(依赖注入Dependency Injection)
承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" clas ...
- 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)
参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...
- AngularJS - 依赖注入(Dependency Injection)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...
- MVC使用StructureMap实现依赖注入Dependency Injection
使用StructureMap也可以实现在MVC中的依赖注入,为此,我们不仅要使用StructureMap注册各种接口及其实现,还需要自定义控制器工厂,借助StructureMap来生成controll ...
- 理解依赖注入(Dependency Injection)
理解依赖注入 Yii2.0 使用了依赖注入的思想.正是使用这种模式,使得Yii2异常灵活和强大.千万不要以为这是很玄乎的东西,看完下面的两个例子就懂了. class SessionStorage { ...
随机推荐
- CDN的基本原理和基础架构
CDN基本原理 最简单的CDN网络由一个DNS服务器和几台缓存服务器组成: ①当用户点击网站页面上的内容URL,经过本地DNS系统解析,DNS系统会最终将域名的解析权交给CNAME指向的CDN专用DN ...
- Apache 性能配置优化
前言 最近在进行apache性能优化设置.在修改apache配置)文件之前需要备份原有的配置文件夹conf,这是网站架设的好习惯.以下的apache配置调优均是在red had的环境下进行的. htt ...
- python的多路复用实现聊天群
在我的<python高级编程和异步io编程>中我讲解了socket编程,这里贴一段用socket实现聊天室的功能的源码,因为最近工作比较忙,后期我会将这里的代码细节分析出来,目前先把代码贴 ...
- ubuntu16更新源
http://blog.csdn.net/fengyuzhiren/article/details/54844870
- Final冲刺贡献分
小组名称:Hello World! 项目名称:空天猎 组长:陈建宇 成员:刘成志.刘耀泽.刘淑霞.黄泽宇.方铭.贾男男 一.贡献分数规则: (1)基础分:5 , 4 ,4 , 3 , 2 ,2 ,1. ...
- Task 6.4 冲刺Two之站立会议10
今天是最后一次站立会议,对我们的软件发布进行了讨论,看如何发布软件.我主要负责编写发布时需要提供的文和资料.
- 我现在对Git的认识
由于时间关系,我还没能真正的了解什么是Git,只是大致的了解了一下,并且在网上查阅了资料,做了一些总结,以便进一步研读. Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项 ...
- 软工1816 · Beta冲刺(3/7)
团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助后端完成历史记录接口.美食排行榜接口 完成食堂平面图的绘制 确定web端业 ...
- “吃神么,买神么”的第一个Sprint计划(第七天)
“吃神么,买神么”项目Sprint计划 ——5.25 星期一(第五天)立会内容与进度 摘要: 所有的部件都完成,在贴每个人负责的部件时发现很多问题,很多网页布局的运用不熟练,一部分的div会跑位置~ ...
- java thread start() 和 run() 区别
1.start() public static void main(String[] args) { // TODO 自动生成的方法存根 Thread t = new Thread() { publi ...