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 ...
随机推荐
- Thinking about think-time functions
You will find yourself very familier to this topic. Ok, let me ask you one question: Let me know th ...
- c语言学习的第6天
#include <stdio.h> int main() { int x=100; if(x==0) { printf("x等于0\n"); printf(" ...
- LevelDB系列之SSTable(Sorted Strings Table)文件
SSTable是Bigtable中至关重要的一块,对于LevelDb来说也是如此,对LevelDb的SSTable实现细节的了解也有助于了解Bigtable中一些实现细节. 本节内容主要讲述SSTab ...
- PHP请求页面
< ?php $file_contents = file_get_contents('http://www.ccvita.com/'); echo $file_contents; ?> 有 ...
- Oracle中建表空间以及用户
第一步:创建临时表空间 --创建临时表空间-- CREATE TEMPORARY TABLESPACE JSYCCS_TEMP ---'JSYCCS_TEMP'临时表空间名 TEMPFILE 'E ...
- Eclipse 4.6 Neon, could not create the java virtual machine
下了eclipse 4.6,打开报错:could not create the java virtual machine. a fatal exception has occurred. 命令行用 e ...
- IOS学习:在工程中添加百度地图SDK
1.将下载下来的sdk中的inc文件夹.mapapi.bundle.libbaidumapapi.a添加到工程中,其中libbaiduapi.a有两个,一个对应模拟器一个对应真机,导入方法如下: 第一 ...
- openstack的第二天
今天,在公司测试了还是网络有问题. 但是用了rdo还是成功了~明天就再试试怎么开放端口进来.
- PHP 学习笔记 01
例子: 为什么要学PHP 主观原因: 前段时间在学校处理了毕业的一些事情,回到上海后开始了找工作的旅程.意向工作是WPF开发或者ASP.NET 作为后端的WEB开发. 陆陆续续一直在面试,其中有一家公 ...
- Collection中的排序
我们来了解一下Collection的框架与接口: Set接口下面已经有SortedSet接口,其中提供了很多自带排序的实现类,例如ThreeSet,用户还能够自定义比较器来规定自己的排序规则. 本篇着 ...