Spring Setter Injection and Constructor Injection
Setter Injection
AppContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageRenderer"
class="com.apress.prospring4.ch3.xml.StandardOutMessageRenderer"
p:messageProvider-ref="messageProvider"/> <bean id="messageProvider"
class="com.apress.prospring4.ch3.xml.HelloWorldMessageProvider"/>
</beans>
Source Code
package com.apress.prospring4.ch3; public interface MessageRenderer {
void render();
void setMessageProvider(MessageProvider provider);
MessageProvider getMessageProvider();
}
package com.apress.prospring4.ch3; public interface MessageProvider {
String getMessage();
}
package com.apress.prospring4.ch3.xml; import com.apress.prospring4.ch3.MessageProvider; public class HelloWorldMessageProvider implements MessageProvider {
@Override
public String getMessage() {
return "Hello World!";
}
}
package com.apress.prospring4.ch3.xml; import com.apress.prospring4.ch3.MessageProvider;
import com.apress.prospring4.ch3.MessageRenderer; public class StandardOutMessageRenderer implements MessageRenderer {
private MessageProvider messageProvider; @Override
public void render() {
if (messageProvider == null) {
throw new RuntimeException(
"You must set the property messageProvider of class:"
+ StandardOutMessageRenderer.class.getName());
} System.out.println(messageProvider.getMessage());
} @Override
public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
} @Override
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
package com.apress.prospring4.ch3; import org.springframework.context.support.GenericXmlApplicationContext; public class DeclareSpringComponents {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-xml.xml");
ctx.refresh(); MessageRenderer messageRenderer = ctx.getBean("messageRenderer",
MessageRenderer.class); messageRenderer.render();
}
}
Constructor Injection
AppContext.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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageProvider"
class="com.apress.prospring4.ch3.xml.ConfigurableMessageProvider"
c:message="This is a configurable message"/> <bean id="constructorConfusion"
class="com.apress.prospring4.ch3.xml.ConstructorConfusion">
<constructor-arg type="int">
<value>90</value>
</constructor-arg>
</bean>
</beans>
Source code
package com.apress.prospring4.ch3; public interface MessageProvider {
String getMessage();
}
package com.apress.prospring4.ch3.xml; import com.apress.prospring4.ch3.MessageProvider; public class ConfigurableMessageProvider implements MessageProvider {
private String message; public ConfigurableMessageProvider(String message) {
this.message = message;
} @Override
public String getMessage() {
return message;
}
}
package com.apress.prospring4.ch3.xml;
import com.apress.prospring4.ch3.MessageProvider; import org.springframework.context.support.GenericXmlApplicationContext; public class ConstructorConfusion {
private String someValue; public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
this.someValue = someValue;
} public ConstructorConfusion(int someValue) {
System.out.println("ConstructorConfusion(int) called");
this.someValue = "Number: " + Integer.toString(someValue);
} public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-xml.xml");
ctx.refresh(); ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
System.out.println(cc); MessageProvider mp = (MessageProvider) ctx.getBean("messageProvider");
System.out.println(mp.getMessage()); } public String toString() {
return someValue;
}
}
Spring Setter Injection and Constructor Injection的更多相关文章
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- [Java Sprint] Spring XML Configuration : Constructor Injection Demo
Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html Now le ...
- 一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) set 注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- Registering Components-->Autofac registration(include constructor injection)
https://autofaccn.readthedocs.io/en/latest/register/registration.html Registration Concepts (有4种方式来 ...
- Spring点滴七:Spring中依赖注入(Dependency Injection:DI)
Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...
- Spring的DI(Dependency Injection)
写在之前,作为正在学习的程序员,对于Spring理解比较差,给两个简单的定义大家看一下. 控制反转(Inversion of Control),是一个重要的面向对象编程的法则来削减计算机程序的耦合问题 ...
- Autofac Property Injection and Method Injection
While constructor parameter injection is the preferred method of passing values to a component being ...
- Windows Dll Injection、Process Injection、API Hook、DLL后门/恶意程序入侵技术
catalogue 1. 引言2. 使用注册表注入DLL3. 使用Windows挂钩来注入DLL4. 使用远程线程来注入DLL5. 使用木马DLL来注入DLL6. 把DLL作为调试器来注入7. 使用c ...
- spring setter方法注入
<bean id="dao" class="Dao"></bean> <bean id="service" c ...
随机推荐
- How to Install JAVA 8 (JDK/JRE 8u111) on Debian 8 & 7 via PPA
Oracle JAVA 8 Stable release has been released on Mar,18 2014 and available to download and install. ...
- mongoDB研究笔记:复制集故障转移机制
上面的介绍的数据同步(http://www.cnblogs.com/guoyuanwei/p/3293668.html)相当于传统数据库中的备份策略,mongoDB在此基础还有自动故障转移的功能.在复 ...
- JedisPool异常Jedis链接处理
问题现象(jedis-2.1.0.jar) 基于JedisPool管理Jedis对象,通过get方法获取值,出现key对应的value值错误,例如: K V a a b b Jedis.get(“a” ...
- http学习笔记(四)——HTTP报文
http报文是在http应用程序之间发送的数据块,这些数据块以一些文本形式的元信息. 请求报文从客户端流入服务器,向服务器请求数据,服务器响应请求,响应报文从服务器流出,回到客户端. 这就构成了一个事 ...
- 实战使用Axure设计App,使用WebStorm开发(5) – 实现页面功能
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(十六):AccessToken自动管理机制
在<Senparc.Weixin.MP SDK 微信公众平台开发教程(八):通用接口说明>中,我介绍了获取AccessToken(通用接口)的方法. 在实际的开发过程中,所有的高级接口都需 ...
- jdk研究——java.lang
jdk研究 volatile 是什么意思? 如何看jdk源码? 如何调试源码!---------仔细解读关键类,关键代码,常用的api的解释! 自己有疑问的不懂地方-------- 不懂的太多怎么办. ...
- linux奇技淫巧 4
压缩解压 tar 即可压缩也可以解压 c 压缩 如果没有z.j参数,则表示,只打包,不压缩. 就说, t 查看 z 以gzip方式压缩 相当于 gzip ?.. j 以bzip方式压缩 bzip2 ? ...
- Lua字符串库(整理)
Lua字符串库小集 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回 ...
- 使用JSExcelXML.js导出Excel模板
github地址:https://github.com/464884492/JSExcelXml 业务系统显示效果图 导出模板图 功能描述 世间万物总是相生相克,既然我们的客户要求有导出Ex ...