[学习笔记]Spring依赖注入
依赖:
典型的企业应用程序不可能由单个对象(在spring中,也可称之bean)组成,再简单的应用也是由几个对象相互配合工作的,这一章主要介绍bean的定义以及bean之间的相互协作。
依赖注入:
spring中的依赖注入(Dependency injection (DI))主要有两种形式:构造器注入和setter方法注入。
构造器注入:
基于构造函数的方式有其自己的优势,它可以明白地创建出带有特定构造參数的对象。另外它对于创建一些在类中不须要常常变化的域有明显的优势。假设用setter方法来做这样的事情会显得非常不协调。但通常可以採用init的方法在创建时就将其初始化。
当然对于某些类可能有非常多的域,构造函数不可能包括全部的情况,并且当中可以包括的构造參数也是有限的,此时Setter方法注入就可以以发挥其余地。
下面演示样例是一个仅仅能通过构造器注入的类:
- public class SimpleMovieLister {
- // the SimpleMovieLister has a dependency on a MovieFinder
- private MovieFinder movieFinder;
- // a constructor so that the Spring container can 'inject' a MovieFinder
- public SimpleMovieLister(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- // business logic that actually 'uses' the injected MovieFinder is omitted...
- }
构造函数參数匹配时根据的是构造器參数类型,为了不产生歧义,一般构造參数给出的顺序依照构造函数中參数给定的顺序。例如以下:
- package x.y;
- public class Foo {
- public Foo(Bar bar, Baz baz) {
- // ...
- }
- }
没有潜在的歧义存在,如果Bar和Baz两个类不想关
- <span style="color:#333333;"><beans>
- <bean id="foo" class="x.y.Foo">
- <</span><span style="color:#ff0000;">constructor-arg</span><span style="color:#333333;"> ref="bar"/>
- <constructor-arg ref="baz"/>
- </bean>
- <bean id="bar" class="x.y.Bar"/>
- <bean id="baz" class="x.y.Baz"/>
- </beans></span>
当还有一个bean被引用时,假设类型一直,匹配就能够发生。当一个简单类型使用时,比如<value>true<value>,Spring不能决定value的类型。就不能进行匹配。
看以下一个演示样例:
- package examples;
- public class ExampleBean {
- // No. of years to the calculate the Ultimate Answer
- private int years;
- // The Answer to Life, the Universe, and Everything
- private String ultimateAnswer;
- public ExampleBean(int years, String ultimateAnswer) {
- this.years = years;
- this.ultimateAnswer = ultimateAnswer;
- }
- }
在前面这个演示样例中,使用type属性。容器能够进行简单的类型匹配:
- <bean id="exampleBean" class="examples.ExampleBean">
- <constructor-arg type="int" value="7500000"/>
- <constructor-arg type="java.lang.String" value="42"/>
- </bean>
相同。我们也能够使用index属性来指定參数顺序(注意index从0開始):
- <bean id="exampleBean" class="examples.ExampleBean">
- <constructor-arg index="0" value="7500000"/>
- <constructor-arg index="1" value="42"/>
- </bean>
在spring3.0中,我们也能够使用构造器參数名字来制定相应的參数值:
- <bean id="exampleBean" class="examples.ExampleBean">
- <constructor-arg name="years" value="7500000"/>
- <constructor-arg name="ultimateanswer" value="42"/>
- </bean>
注意:Keep in mind that to make this work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor.
If you can't compile your code with debug flag (or don't want to) you can use @ConstructorProperties
JDK
annotation to explicitly name your constructor arguments. The sample class would then have to look as follows:
- package examples;
- public class ExampleBean {
- // Fields omitted
- @ConstructorProperties({"years", "ultimateAnswer"})
- public ExampleBean(int years, String ultimateAnswer) {
- this.years = years;
- this.ultimateAnswer = ultimateAnswer;
- }
- }
setter方法注入:
下面是一个简单的仅仅能用setter方法进行注入的样例:
- public class SimpleMovieLister {
- // the SimpleMovieLister has a dependency on the MovieFinder
- private MovieFinder movieFinder;
- // a setter method so that the Spring container can 'inject' a MovieFinder
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- // business logic that actually 'uses' the injected MovieFinder is omitted...
- }
ApplicationContext
支持setter注入和构造器注入,也支持在已存在构造器注入的情况下继续进行setter注入。
依赖注入实例:
下面样例为setter注入的xml配置文件:
- <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;
- }
- }
上面这个样例使用的是setter注入。
以下是构造器注入的一个样例:
- <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"/>
[学习笔记]Spring依赖注入的更多相关文章
- Spring学习笔记——Spring依赖注入原理分析
我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...
- SpringMVC:学习笔记(11)——依赖注入与@Autowired
SpringMVC:学习笔记(11)——依赖注入与@Autowired 使用@Autowired 从Spring2.5开始,它引入了一种全新的依赖注入方式,即通过@Autowired注解.这个注解允许 ...
- Spring.NET学习笔记6——依赖注入(应用篇)
1. 谈到高级语言编程,我们就会联想到设计模式:谈到设计模式,我们就会说道怎么样解耦合.而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny I ...
- Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)
什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...
- AngularJS学习笔记之依赖注入
最近在看AngularJS权威指南,由于各种各样的原因(主要是因为我没有money,好讨厌的有木有......),于是我选择了网上下载电子版的(因为它不要钱,哈哈...),字体也蛮清晰的,总体效果还不 ...
- 再学习之Spring(依赖注入)
一.概述 Spring框架是以 简化Java EE应用程序的开发 为目标而创建的.Spring可以实现很多功能,但是这些功能的底层都依赖于它的两个核心特性,也就是依赖注入和面向切面编程.几乎Sprin ...
- Unity学习笔记(4):依赖注入
Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍 1:构造函数注入 1.1当类有多个构造函数时,可以通过InjectionConstructor特性 ...
- angular2 学习笔记 ( DI 依赖注入 )
refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...
- Angular4.0从入门到实战打造在线竞拍网站学习笔记之三--依赖注入
Angular4.0基础知识之组件 Angular4.0基础知识之路由 依赖注入(Dependency Injection) 正常情况下,我们写的代码应该是这样子的: let product = ne ...
随机推荐
- windows系统上安装Redis,并且设置Redis密码
一.Windows版本的Redis下载 下载地址:https://github.com/MSOpenTech/redis/releases 我下载的是最新版的3.2 二.安装Redis 我下载的是安装 ...
- Mahout0.6-VectorDumper bug修复
VectorDumper类的功能是从SequenceFile中按照键值对的方式读取信息并将其转化为文本形式,具体使用见第五部分1.1.2节第3)条.如果不对源码进行修改使用时存在两个bug,现在只对b ...
- HDU4666+POJ2926【最远曼哈顿距离】
一开始就明白那个N*1<k的算法了, 可无奈删除操作耗时还是太多,最后学习了STL set,map相应的用法,方便好多. STL真的是一个好工具 #include<iostream> ...
- HTML二(基本标签)
一.标题 HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <!--标题--> <h1>标题 1</h1> ...
- vb sendmessage 详解1
SendMessage函数的常用消息及其应用(有点长,希望能对大家有所帮助)函数原型: Declare Function SendMessage Lib "user32" Alia ...
- SqlServer数据库分离与附加
SQL Server提供了“分离/附加”数据库.“备份/还原”数据库.复制数据库等多种数据库的备份和恢复方法.这里介绍一种学习中常用的“分离/附加”方法,类似于大家熟悉的“文件拷贝”方法,即把数据库文 ...
- this词法
1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...
- Windows 消息机制浅析
1. Windows 的历史 中国人喜欢以史为鉴,而事实也确实是,如果你能知道一件事情的来龙去脉,往往可以更容易地理解事物为什么会表现为当前这样的现状.所以,我的介绍性开场白通常会以一段历 ...
- 微信小程序:bindtap方法传参
1.wxml <view bindtap="pay_again" data-name="{{orderList.jid}}" data-fee=" ...
- HDUOJ-----1085Holding Bin-Laden Captive!
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...