小菜鸟学 Spring-Dependency injection(二)
注入方式一:set注入
<bean id="exampleBean" class="examples.ExampleBean"> <!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property> <!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean { private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i; public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
} public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
} public void setIntegerProperty(int i) {
this.i = i;
}
}
注入方式二:构造注入
<bean id="exampleBean" class="examples.ExampleBean"> <!-- constructor injection using the nested <ref/> element -->
<constructor-arg>
<ref bean="anotherExampleBean"/>
</constructor-arg> <!-- constructor injection using the neater 'ref' attribute -->
<constructor-arg ref="yetAnotherBean"/> <constructor-arg type="int" value="1"/>
</bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean { private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i; public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
注入方式三:静态工厂方法注入
<bean id="exampleBean" class="examples.ExampleBean"
factory-method="createInstance">
<constructor-arg ref="anotherExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean { // a private constructor
private ExampleBean(...) {
...
}
// a static factory method; the arguments to this method can be
// considered the dependencies of the bean that is returned,
// regardless of how those arguments are actually used.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { ExampleBean eb = new ExampleBean (...);
// some other operations...
return eb;
}
}
注入方式四:自动装配
小菜鸟学 Spring-Dependency injection(二)的更多相关文章
- 小代学Spring Boot之数据源
想要获取更多文章可以访问我的博客 - 代码无止境. 经过一天对Spring Boot的研究,小代同学已经对Spring Boot框架有了一个大概的认识.并且还创建了一个简单的Spring Boot的W ...
- 小代学Spring Boot之集成MyBatis
想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...
- 小代学Spring Boot之自定义Starter
想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...
- Spring Dependency Injection浅析
Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件. 1.在UserService中提供一个get/set的name方法, ...
- 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构
前几天向大家介绍了一种用工具类生成数据表的方法,只是之前的方法须要使用一个跟项目关系不大的工具类.不免让人认为有些多余,所以呢.今天再向大家介绍一种方法.即Hibernate与Spring配合生成表结 ...
- 小菜鸟 学MQ(二)
mq服务启动以后 接着要做的事情就是 [发送]和[接受]消息. 首先有两种不同类型的Message:Topic,Queue 第一种Topic JMS规范定义了,Topic需要实现 发布和订阅两个功能, ...
- 小代学Spring Boot之开篇
想要获取更多文章可以访问我的博客 - 代码无止境. 前情提要 小代是一名入职不久的程序员,公司同事都亲切的称他小代.有一天小代的老大陈BOSS和小代说,公司后端最近准备换技术框架了. 小代: 换成啥? ...
- 小菜鸟学Spring-读取属性文件值(三)
Example: the PropertyPlaceholderConfigurer 属性配置文件内容如下所示: jdbc.driverClassName=org.hsqldb.jdbcDriver ...
- 小菜鸟学 Spring-bean scope (一)
this information below just for study record of mine. 默认情况下:Spring 创建singleton bean 以便于错误能够被发现. 延迟加载 ...
随机推荐
- NOIP2012普及组 (四年后的)解题报告 -SilverN
本章施工仍未完成 现在的时间是3.17 0:28,我困得要死 本来今天(昨天?)晚上的计划是把整个四道题的题解写出来,但是到现在还没写完T4的高效算法,简直悲伤. 尝试了用floyd写T4,终于大功告 ...
- 莫比乌斯反演 BZOJ 2820
莫比乌斯反演真(TMD)难学.我自看了好长时间. BZOJ 2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1384 Sol ...
- Cg关键字(keywords)
保留标识符 除了下面列出的,任何以两个的下划线作为前缀(例如,__ newType)的标识符被保留. 注意,矩阵(matrix)和向量类型(vector types)(如half2x3或float4) ...
- Lambda 表达式(C# 编程指南)
Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数.Lambda 表达式对于编写 LINQ 查 ...
- Jedis下的ShardedJedis(分布式)使用方法(二)
上一篇中介绍了ShardedJedis的基本使用方法以及演示了一个简单的例子,在这一篇中我们来介绍了ShardedJedis的原理. 1.ShardedJedis内部实现 首先我们来看一下Sharde ...
- java14-9 Doteformat的练习
需求: 键盘录入出生年月日,计算出距离现在已经生活了几天 分析: A:创建键盘录入固定模式的字符串 B:计算步骤: a:把输入进来的字符串格式化成日期 b:获取现在的日期,减去格式化后的日期 c:把得 ...
- copy sqlserver中DATE类型的数据转化 CONVERT
copy http://www.cnblogs.com/benwu/p/3939044.html 主要描述的是SQL Server使用convert取得datetime日期数据的实际操作流程,在实际操 ...
- U3D rootMotion
Body Transform The Body Transform is the mass center of the character. It is used in Mecanim's retar ...
- DEDECMS中直接通过数据库插入文章
突然想用爬虫爬来的数据对接DEDECMS,所以研究了一下怎么直接在DEDECMS中直接插入一片文章 (但其实我并没有这样用.因为我直接用前台做的网页,用ajax请求的json数据,其他的都简单,就是没 ...
- ffmpeg常用基本命令(转)
[FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...