spring--注入类型--构造方法(不常用)
3.3.1.1. Constructor Injection
Constructor-based DI is effected by invoking a constructor with a number of arguments, each representing a dependency. Additionally, calling a static
factory method with specific arguments
to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to a static
factory method similarly. Find below an example of a class that could only
be dependency injected using constructor injection. Notice that there is nothing special about this class.
public class SimpleMovieLister { // theSimpleMovieLister
has a dependency on aMovieFinder
private MovieFinder movieFinder; // a constructor so that the Spring container can 'inject' aMovieFinder
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
} // business logic that actually 'uses' the injectedMovieFinder
is omitted...
}
There is no potential for ambiguity here (assuming of course that Bar
and Baz
classes are not related in an inheritance hierarchy). Thus the following configuration will work just fine, and you do
not need to specify the constructor argument indexes and / or types explicitly.
<beans>
<bean name="foo" class="x.y.Foo">
<constructor-arg>
<bean class="x.y.Bar"/>
</constructor-arg>
<constructor-arg>
<bean class="x.y.Baz"/>
</constructor-arg>
</bean>
</beans>
When another bean is referenced, the type is known, and matching can occur (as was the case with the preceding example). When a simple type is used, such as<value>true<value>
, Spring cannot determine the type of the value, and so
cannot match by type without help. Consider the following class:
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- udaoimpl is a UserDAOImpl Object-->
<bean id="udaoimpl" class="com.bjsxt.dao.impl.UserDAOImpl">
</bean> <!-- userService is a UserService Object-->
<bean id="userService" class="com.bjsxt.service.UserService">
<!-- 最常用的setter方法注入 -->
<!--userService's setUserDAO method DI(注入) 之前产生的udaoimpl对象-->
<!-- <property name="userDAO"><ref bean="udaoimpl"/></property>
-->
<!-- 不推荐,可以忘记‘’‘’ userService的构造方法注入 -->
<constructor-arg>
<ref bean="udaoimpl"/>
</constructor-arg>
<!--<property name="userDAO" ref="udaoimpl"/> -->
</bean> </beans>
版权声明:本文为博主原创文章,未经博主允许不得转载。
spring--注入类型--构造方法(不常用)的更多相关文章
- Spring构造方法注入类型歧义
在Spring框架中,当一个类包含多个构造函数带的参数相同,它总是会造成构造函数注入参数类型歧义的问题. 问题 让我们来看看这个客户 bean 实例.它包含两个构造方法,均接受3个不同的数据类型参数. ...
- Spring容器三种注入类型
Spring注入有三种方式: 1.Set注入(使用最多) 2.构造器注入(使用不多) 3.接口注入(几乎不用)不做测试了 1.Set注入:所谓Set注入就是容器内部调用了bean的Set***方法,注 ...
- Spring属性注入、构造方法注入、工厂注入以及注入参数(转)
Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...
- spring之注入类型
spring有三种注入类型: set注入: 构造注入: 接口注入: 一.set注入(引用spring官方文档中的例子)(用的最多) 1.首先在代码中我们需要编写成员变量的set方法,如下所示,一般情况 ...
- Spring再接触 注入类型
共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...
- spring集合类型注入
spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...
- spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...
- Spring 注入简介
注入方式有三种,setter,构造方法,接口注入. 常用的是setter注入和构造方法注入. setter注入: <?xml version="1.0" encodi ...
- Spring注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- Spring注入中byType和byName的总结
1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...
随机推荐
- iPhone和iPad版本的分辨率a
- PeopleSoft Object Types Definitions
PeopleSoft stores object definitions types such as Record, Field and SQL definitions as numbers in ...
- 【转】准确理解CSS clear:left/right的含义及实际用途
零.说点什么 好久没更新了.并不是在折腾什么大作,而是广度学习与实践中,加上婚礼等诸多大事,所以产出较少. 今天这篇也只是小作,博客是自己很好的学习工具,只要我学习不止,博客也会不断更新的. 我们平时 ...
- 《安全参考》HACKCTO-201312-12
小编的话 “忽如一夜春风来,千树万树梨花开.” 小伙伴们,不要只为了“千树万树的梨花”而惊喜,陶醉! 与此同时,您最爱的整合型信息安全技术期刊<安全参考>第12期也如约而至啦! 这一期&l ...
- 表格控件表头栏目(Column)与数据表头步
不用手工增加栏目的列,也就是Column,由数据库的查询结果自动创建. 用的是Delphi2010,安装了Dev,用CxGrid显示数据库查询结果.用什么控件没有关键,道理相同的.
- DevExpress 表中数据导出
gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...
- JS中的集中页面跳转的方法
第一种: <script language="javascript" type="text/javascript"> wi ...
- matlab封装DLL混合编程总结
最近做了个项目要用到matlab做些算法处理,然后用.net项目调用这个类,我把这个matlab封装dll总结了下如下: matlab是商业数学软件,优势是在算法开发上面有很强的功能,提供了很多数学算 ...
- MvvmCross for WPF 支持子窗体显示、关闭、传参
最近在做 PCL(Portable Class Library)平台的项目,所以发一下自己遇到的问题 MvvmCross 是 PCL 平台的一个 MVVM 框架 地址:https://github.c ...
- 【J2EE】Hibernate
Hibernate是面向Java环境的对象/关系数据库映射工具,管理Java应用和数据库之间的映射关系,提供数据查询和获取数据的方法,可以大幅减少使用JDBC处理数据持久化的时间. 使用Eclipse ...