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. 13-调试Dockerfile

    包括 Dockerfile 在内的任何脚本和程序都会出错.有错并不可怕,但必须有办法排查,所以本节讨论如何 debug Dockerfile. 先回顾一下通过 Dockerfile 构建镜像的过程: ...

  2. java查询几个菜单下的所有下级菜单

    需求: 假如有几个一级菜单,一级菜单下面有几个二级菜单,二级菜单下又还有三级菜单.现在要求一级菜单里面的几个设置为无效,将不显示在前端.现在需要的是查询出一级菜单下面所有的菜单,包括二级,三级菜单 原 ...

  3. CHAPTER 5 ‘The Master of Those Who know’ Aristotle 第5章 “有识之士的大师” 亚里士多德

    CHAPTER 5 ‘The Master of Those Who know’ Aristotle 第5章 “有识之士的大师” 亚里士多德 ‘All men by nature desire to ...

  4. 琴声不等式--jensen

    (来自百度百科) 1. 凹函数,不加权 2. 凹函数,加权 3. 凸函数,不加权 4. 凸函数,加权 应用 在EM算法Q函数的推导中,用到了第二个不等式(凹函数,加权)

  5. SpringBoot集成dubbo实例

    项目总览图: 最下面有项目的pom,具体内容: 项目运行注意事项: 先启动 provider, 将providers.xml中 port 先修改为20187 执行test目录 下的DubboProvi ...

  6. Sqlserver 每日订单半小时数据统计

    ) '订单数' FROM (SELECT CASE THEN ), create_at, ) ),DATEPART(hh, create_at))+':00:00') ELSE ), create_a ...

  7. spring boot开启热部署

    步骤一:添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  8. win10系统下载-靠谱推荐

    win10系统下载的靠谱推荐: 1.http://www.xitongtiandi.net/wenzhang/win10/12926.html 2.https://msdn.itellyou.cn/ ...

  9. Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射

    一.多表的一个关联关系 老师和学生是一对多的关系 student:tid属性 外键约束 对应teacher表中的id属性 teacher:id 在myeclipse的db窗口中选中两个表来生成类.   ...

  10. myeclipse 导入 import maven web project

    用google才收到了这个.. http://stackoverflow.com/questions/12197662/maven-java-web-project-not-recognised-wh ...