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 staticfactory 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 {

    // 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...
}

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--注入类型--构造方法(不常用)的更多相关文章

  1. Spring构造方法注入类型歧义

    在Spring框架中,当一个类包含多个构造函数带的参数相同,它总是会造成构造函数注入参数类型歧义的问题. 问题 让我们来看看这个客户 bean 实例.它包含两个构造方法,均接受3个不同的数据类型参数. ...

  2. Spring容器三种注入类型

    Spring注入有三种方式: 1.Set注入(使用最多) 2.构造器注入(使用不多) 3.接口注入(几乎不用)不做测试了 1.Set注入:所谓Set注入就是容器内部调用了bean的Set***方法,注 ...

  3. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  4. spring之注入类型

    spring有三种注入类型: set注入: 构造注入: 接口注入: 一.set注入(引用spring官方文档中的例子)(用的最多) 1.首先在代码中我们需要编写成员变量的set方法,如下所示,一般情况 ...

  5. Spring再接触 注入类型

    共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...

  6. spring集合类型注入

    spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...

  7. spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...

  8. Spring 注入简介

    注入方式有三种,setter,构造方法,接口注入.   常用的是setter注入和构造方法注入.   setter注入: <?xml version="1.0" encodi ...

  9. Spring注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  10. Spring注入中byType和byName的总结

    1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...

随机推荐

  1. 软件工程 speedsnail 冲刺8

    完成任务:蜗牛移动的界面调整,速度调整: 遇到问题: 问题1 进入关卡的开始蜗牛会跳一段距离 解决1 线程延迟3S: 明日任务: 划线实现挡板功能

  2. 安装CMS遇到php5.3的问题

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  3. 合并多个List<T>类型并通过LINQ按指定属性排序

    后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...

  4. CSS中的content和attr的用法

    问题缘起 在闲看别人网站时发现了这样的代码 <span class="hamburger icon" data-icon="C"> .icon:be ...

  5. yii在TbGridView的td里面加入相应的下拉选项(转)

    当你需要在一个GridView渲染某一个复杂元素的时候(特别是在这种情况下,这是一个小部件),这就需要你在控制器中调用一个动作.例如你给一个GridView的定义这样的一列: <?php $th ...

  6. NLP自然语言处理学习笔记三(集成开发环境)

    前言: 我们在做自然语言学习的过程中使用Python进行编程.是用解析器的方式确实有些麻烦.在这里给大家推荐一款集成开发环境IDE可以很方便的对Python进行项目管理,代码自动提示,运行调试等. 简 ...

  7. python 从private key pem文件中加载public key

    import rsa import logging from Crypto.PublicKey import RSA class RsaUtil: def __init__(self, pem_fil ...

  8. python去掉空行

    #用strip(),split()两个方法都可以判断空行 infile=open('/.../','r') outfile=open('/.../','w') for li in infile.rea ...

  9. JavaScrip拖动动画中的常见BUG

    经常我们在用JS辛苦写完一个拖动效果之后 ,发现有各种无法用JS解决的BUG.比如拖动时DOM元素中的内容会变蓝,鼠标的指示会变为一个小+号,或disable的样式,通常这种情况一发生,我们的拖动效果 ...

  10. 《Prism 5.0源码走读》 设计模式

    Prism或Prism构建的应用程序时会使用大量的设计模式,本文简要列举Prism相关的那些设计模式. Adapter(适配器模式):Prism Library主要在Region和IoC contai ...