[学习笔记]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 ...
随机推荐
- IIS 之 Web 服务器上的 ASP.NET 进程模型设置
配置 Microsoft Internet 信息服务 (IIS) Web 服务器上的 ASP.NET 进程模型设置. processModel 节只能在 Machine.config 文件中进行设置, ...
- ORACLE NVL 和 NVL2 函数的使用
NVL函数是一个空值转换函数,在SQL查询中主要用来处理null值.在不支持 null 值或 null 值无关紧要的情况下,可以使用 NVL( ) 来移去计算或操作中的 null 值. Oracle在 ...
- linux远程
apt-get install rdesktop $rdesktop -u administrator -p ****** -a 16 192.168.1.1 //都直接登陆了,
- 解决Cydia出现红字提示“Sub-process/usr/bin/dpkg returned an error code(2)
进入此路径/var/lib/dpkg/,修改红框中的文件名,依次修改为:"available" 重新命名为 "available-bak":"stat ...
- sqlserver profiler 抓出来作业的代码 SQLAgent - TSQL JobStep,二进制作业名字转化为字段串作业名字,job_id
sqlserver 中 profiler 抓出来不少是作业中的代码,applicationname 类似于 SQLAgent - TSQL JobStep (Job 0x94B9B5C016E8D94 ...
- oracle 批量更新之将一个表的数据批量更新至另一个表
oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...
- Oracle快速克隆安装
Oracle的家目录进行快速克隆,对同类型机器配置很高效的! 01.确认你在克隆数据库时,原数据库已经关闭 sqlplus / as sysdba; shutdown immediate ; ...
- 在交叉编译中使用最新版的SS
因为旧版本的ss-local总是出现 shake hands failed 错误, 打算用最新的版本试试, 所以尝试在编译中使用最新版的shadowsocks. 项目地址 Shadowsocks-li ...
- 【vue.js】windows下安装vue.js
windows下搭建vue开发环境 Vue.js是一套构建用户界面的 “渐进式框架”.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常 ...
- pip安装注意事项
pip源 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...