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)的更多相关文章

  1. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  2. 简明依赖注入(Dependency Injection)

    前言 这是因特奈特上面不知道第几万篇讲依赖注入(Dependency Injection)的文章,但是说明白的却寥寥无几,这篇文章尝试控制字数同时不做大多数. 首先,依赖注入的是一件很简单的事情. 为 ...

  3. 14.AutoMapper 之依赖注入(Dependency Injection)

    https://www.jianshu.com/p/f66447282780   依赖注入(Dependency Injection) AutoMapper支持使用静态服务定位构建自定义值解析器和自定 ...

  4. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

  5. Spring之对象依赖关系(依赖注入Dependency Injection)

    承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" clas ...

  6. 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)

    参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...

  7. AngularJS - 依赖注入(Dependency Injection)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...

  8. MVC使用StructureMap实现依赖注入Dependency Injection

    使用StructureMap也可以实现在MVC中的依赖注入,为此,我们不仅要使用StructureMap注册各种接口及其实现,还需要自定义控制器工厂,借助StructureMap来生成controll ...

  9. 理解依赖注入(Dependency Injection)

    理解依赖注入 Yii2.0 使用了依赖注入的思想.正是使用这种模式,使得Yii2异常灵活和强大.千万不要以为这是很玄乎的东西,看完下面的两个例子就懂了. class SessionStorage { ...

随机推荐

  1. 浏览器初始页面设置及被hao123劫持解决办法

    最近在用浏览器时打开初始页面都是hao123,喵喵喜欢简单干净的页面,就去设置初始页面. 此处放置初始页面参考(并不太难): https://jingyan.baidu.com/article/11c ...

  2. Lwip:原生态的Linux socket应用如何移植到Lwip上

    lwIP - A Lightweight TCP/IP stack 在上一篇中,我们了解到在OpenFastPath上如何移植原生态的Linux Socket应用程序,那么,对于另外一个老牌的小型TC ...

  3. Docker 私有仓库方案比较与搭建

    我们知道docker镜像可以托管到dockerhub中,跟代码库托管到github是一个道理.但如果我们不想把docker镜像公开放到dockerhub中,只想在部门或团队内部共享docker镜像,能 ...

  4. Django之自带认证

    自带登录实例 {% extends "layout/base.html" %} // 所有link {% block body %} <div id="contai ...

  5. Scrum立会报告+燃尽图(Final阶段第二次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...

  6. Final阶段中间产物

    空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...

  7. iOS 开发学习-类的创建与实现,与java语言的对比

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { //在{}中定义属性(全局变量/实例变量 ...

  8. CS小分队第二阶段冲刺站立会议(6月2日)

    昨日成果:攻克了按钮移动的问题: 遇到问题:一开始按钮移动时候,非常慢,因为是根绝相对位移差来移动,延时很严重,后来改用用鼠标的位置作为按钮的移动位置,效果明显. 按钮的mousedown事件和mou ...

  9. “私人助手”Beta版使用说明

    私人助手(Beta)版使用说明 私人助手这款软件是通过添加事件提醒功能,让用户能在正确的时间做正确的事情,使得工作变得更有效率,而这款软件的特色在于提醒模式的添加,用户可以通过震动.铃声提醒,我们的特 ...

  10. 乱码之UTF-8 &GBK

    在提交JSP时对于乱码问题,首先我们要搞清楚为什么会出现乱码? 看JSP的头文件:<%@ page contentType="text/html;charset=UTF-8" ...