小菜鸟学 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 以便于错误能够被发现. 延迟加载 ...
随机推荐
- UESTC 881 神秘绑架案 --二维DP
LRJ黑书上的例题. 化简均方差公式: 均值的平方一定,所以只需让矩形的总分的平方和最小即可. 定义:dp[k][x1][y1][x2][y2],以(x1,y1)为左上角坐标,(x2,y2)为右下角坐 ...
- HDU 5033 Building --离线+单调栈
题意:给一些建筑物,x表示横坐标,h表示高度,然后查询某些坐标x,问从该点看向天空的最大张角是多大. 解法:离线操作,读入所有数据,然后按x升序排序,对每一个查询的x,先从左到右,依次添加x坐标小于x ...
- Android组件---四大布局的属性详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...
- Update和LateUpdate的区别
LateUpdate晚于所有Update执行 在圣典里LateUpdate被解释成一句话:LateUpdate是在所有Update函数调用后被调用.这可用于调整脚本执行顺序. 当物体在Update里移 ...
- 关于tcpdump抓包一个很详细的介绍
http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html
- 工厂模式(Factory Patter)
1.工厂模式简介 工厂模式属于创建型模式,是专门用来创建对象的模式,抽象了实例化的过程.工厂模式分为 : 工厂方法模式.抽象工厂模式. 在学习工厂方法模式.抽象工厂之前,首先先要了解一下简单工厂模式, ...
- [原创]CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...
- 17SpringMvc_在业务控制方法中写入包装User的模型来收集参数——解决问题
在解决问题之前,我要说明一下jsp页面上填入信息,一个用户的信息比如用户的名字,用户的电话,用户的手机等等,在这个jsp页面上填好信息后,转到有个action处理这个信息.原理是什么? 在jsp页面上 ...
- 思科简单教程CCNA
这是CCNA的内容,从PC配置交换机(或者路由器),这里呢我们使用的软件叫pack 这是ciso开发的一款工具,能生动形象的模拟现实生活中组网技术的过程,下面我大概讲一下流程,想更多的了解我会录制一些 ...
- Discuz! X的CSS加载机制
首先,每个页面都会加载以下两个css,data/cache/style_1_common.css和data/cache/style_1_forum_index.css.先讲讲这两个文件名的命名规则:第 ...