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 { ...
随机推荐
- WebGL实现sprite精灵效果的GUI控件
threejs已经有了sprite插件,这就方便了three的用户,直接可以使用threejs的sprite插件来制作GUI模型.sprite插件是阿里的lasoy老师改造过的,这个很厉害,要学习一哈 ...
- zookeeper简易配置及hadoop高可用安装
zookeeper介绍 是一个分布式服务的协调服务,集群半数以上可用(一般配置为奇数台), 快速选举机制:当集群中leader挂掉,所有小弟会投票选举出新的leader. ...
- centos7以上安装python3,一条命令搞定。
直接复制下面的命令就搞定 yum install python34 python34-pip python34-setuptools 使用方法: python3 ---.py pip3 install ...
- windows 无法链接 \\ , 拼写错误或者网络有问题,解决方法
1. 楼主首先在网上搜索了一遍问题, 比较全面的回答链接如下http://blog.csdn.net/newizan/article/details/50313137 然而并没有解决问题, 于是反思了 ...
- “Hello World!”团队第七周召开的第六次会议
博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八 .功能说明书 一.会议时间 2017年12月6日 11:20-12:00 二 ...
- 感谢Thunder团队
不知不觉中,团队开发的beta版本都已经结束.开发的路上我们一起解决了很多难题,相互帮助走到了现在. 首先我想感谢组长王航.认真负责合理分配任务,使得我们每次发布都可以顺利并且按时完成.感谢胡佑蓉,李 ...
- java第一次实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程名称:java实验 班级:1352 姓名:潘恒 学号:20135209 成绩: ...
- 《JavaScript》字符转义
escape/unescape encodeURIComponent/decodeURIComponent encodeURI/decodeURI 转义函数会对一些 特殊字符进行转义编码 英文.数字. ...
- 【数据预处理】TIMIT语料库WAV文件转换
1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...
- 读书笔记 之java编程思想
本阶段我正在读java的编程思想这本书,这本书只是刚读了第一章的一部分,有些有些要记得所以记录下来, 我认为要记得有就是复用这样可以对对象进行增强,将一个类作为下一个类中基本类型,这样达到的服用的目的 ...