Spring基于Setter函数的依赖注入(DI)
当容器调用一个无参的构造函数或一个无参的静态factory方法来初始化你的bean后,通过容器在你的bean上调用Setter函数,基于Setter函数的DI就完成了。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testbeansetter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testbeansetter</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies>
</project>
SpellChecker.java:
package com.jsoft.testspring.testbeansetter;
public class SpellChecker {
public SpellChecker(){
System.out.println("SpellChecker无参数构造函数初始化");
}
public void checkSpelling(){
System.out.println("SpellChecker检查方法");
}
}
TextEditor.java:
package com.jsoft.testspring.testbeansetter;
public class TextEditor {
private SpellChecker spellChecker;
public void setSpellChecker(SpellChecker spellChecker){
System.out.println("TextEditor通过setter初始化");
this.spellChecker = spellChecker;
}
public void spellCheck() {
this.spellChecker.checkSpelling();
}
}
beans.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="spellChecker" class="com.jsoft.testspring.testbeansetter.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testbeansetter.TextEditor">
<property name="SpellChecker" ref="spellChecker"></property>
</bean> </beans>
唯一的区别就是在基于构造函数注入中,我们使用的是<constructor-arg>标签,而在基于Setter函数的注入中,我们使用的是<property>标签。
第二个你需要注意的点是,如果你要把一个引用传递给一个对象,那么你需要使用ref属性,而如果你要直接传递一个值,那么你应该使用value属性。
App.java:
package com.jsoft.testspring.testbeansetter; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");
textEditor.spellCheck();
}
}
运行结果:

下面将介绍使用p-namespace实现XML配置:
如果你有许多的Setter函数方法,那么在XML配置文件中使用p-namespace是非常方便的。区别如下:
以下为使用<property>标签的配置:
<?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-3.0.xsd"> <bean id="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean> <bean name="jane" class="com.example.Person">
<property name="name" value="John Doe"/>
</bean> </beans>
改用p-namespace之后:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="john-classic" class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane">
</bean> <bean name="jane" class="com.example.Person"
p:name="John Doe">
</bean> </beans>
看起来非常的整洁,在这里,您应该注意到使用p-namespace指定原始值和对象引用的区别。-ref部分表示这不是一个直接的值,而是引用另一个bean。
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test8/testbeansetter
Spring基于Setter函数的依赖注入(DI)的更多相关文章
- spring(3)------控制反转(IOC)/依赖注入(DI)
一.spring核心概念理解 控制反转: 控制反转即IoC (Inversion of Control).它把传统上由程序代码直接操控的对象的调用权交给容器.通过容器来实现对象组件的装配和管理. 所谓 ...
- Spring-Context之六:基于Setter方法进行依赖注入
上文讲了基于构造器进行依赖注入,这里讲解基于Setter方法进行注入.在Java世界中有个约定(Convention),那就是属性的设置和获取的方法名一般是:set+属性名(参数)及get+属性名() ...
- Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)
关于依赖注入, 这篇博文写的非常简单易懂. https://github.com/android-cn/blog/tree/master/java/dependency-injection 此外, 博 ...
- 小白都能看懂的 Spring 源码揭秘之依赖注入(DI)源码分析
目录 前言 依赖注入的入口方法 依赖注入流程分析 AbstractBeanFactory#getBean AbstractBeanFactory#doGetBean AbstractAutowireC ...
- Spring 基于set方法的依赖注入
注意,再次强调,注入一个值用value,注入一个引用,要使用 ref 来注入 同时,注入的对象,要有set和get方法,才能通过方法注入. <?xml version="1. ...
- Spring-基于设置函数的依赖注入
Spring 基于设置函数的依赖注入 当容器调用一个无参的构造函数或一个无参的静态factory方法来初始化你的bean后,通过容器在你的bean上调用设值函数,基于设值函数的DI就完成了. 下面是T ...
- spring学习之依赖注入DI与控制反转IOC
一 Ioc基础 1.什么是Ioc? Ioc(Inversion of Control)既控制反转,Ioc不是一种技术,而是一种思想,在Java开发中意味着将设计好的对象交给容器来进行控制,并不是像传统 ...
- 浅析“依赖注入(DI)/控制反转(IOC)”的实现思路
开始学习Spring的时候,对依赖注入(DI)——也叫控制反转(IOC)—— 的理解不是很深刻.随着学习的深入,也逐渐有了自己的认识,在此记录,也希望能帮助其他入门同学更深入地理解Spring.本文不 ...
- Spring基于构造函数和设值函数的依赖注入
基于构造函数的依赖注入 我们知道,bean标签中指定的类会进行初始化,这个初始化过程中自然会调用构造函数,那我们也可以利用这个构造函数完成依赖注入. 先创建一个类: public class Text ...
随机推荐
- Spring框架 全注解annotation不使用配置文件(SpringConfiguration.java类代替) 补充 xml配置文件没有提示解决
全注解不使用配置文件 首先还是倒包 在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.b ...
- [LUOGU] P2679 子串
一开始用一个f数组转移,发现不太对,状态有重叠部分 f[i][j][k]表示考虑了s的前i位,匹配到t的第j位,用了k个子串,且s的第i位必选 g[i][j][k]表示考虑了s的前i位,匹配到t的第j ...
- (55)zabbix模板嵌套
在zabbix使用过程中,在某些情况下,一个host需要link多个模板.这么做显得比较麻烦,很容易忘记到底要link哪些模板,我想link一个模板就达成这个目标,行不行?然没问题,zabbix模板内 ...
- 七:MYSQL之常用操作符
前言: 运算符连接表达式中各个操作数,其作用是用来指明对操作数所进行的运算. 常见的运算有数学计算.比较运算.位运算及逻辑运算 一:算数运算符 用于各类数值运算.包括加(+).减(-).乘(*).除( ...
- python:post请求业务、调用微信api监控业务
vim post.py #!/usr/bin/env python # -*- coding: utf-8 -*- import json import os import datetime impo ...
- 各种排序算法(JS实现)
目录: 直接插入排序.希尔排序.简单选择排序.堆排序.冒泡排序.快速排序,归并排序.桶排序.基数排序.多关键字排序.总结 JS测试代码 function genArr(){ let n = Math. ...
- 我的第一个ajax脚本
代码如下 //创建XMLHttpRequest对象 var xmlHttp=null; function creatXMLHttp(){ try{ xmlHttp = new XMLHttpReque ...
- cf891a Pride
倘若存在 1,那么答案是 \(n-cnt_1\). 否则,设最短的公约数为 1 的区间长度为 \(minlen\),答案是 \(minlen-1+n-1\). #include <iostrea ...
- ASP.NET Web Application中使用链接文件
最近重构一个内部的平台系统,作为一个平台,其下有几个子系统,每个子系统有自己的网站系统.而每个网站使用的是统一的风格,统一的验证机制,反馈系统,等等.所以,为了避免几个子系统中重复出现相同的资源或文件 ...
- Mac版有道云笔记不能自动同步
删除本地资源文件夹 /Users/xxxx/Library/Containers/com.youdao.note.YoudaoNoteMac 直接删除整个文件夹,之后重新登录账号.