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. 为什么for in循环不适合用于数组

    首先一点无关的,使用(var i in a) 而不是( i in a),除非你想创建全局变量. 第二点,for in 循环会忽略空的数组 var a = []; a[5] = 5; // Perfec ...

  2. Hub control

    Hub(中心) 中心页是用户进入应用的入口点.中心页在丰富的平移视图中显示内容,这样用户一眼就能看见新鲜有趣的内容,从而吸引他们深入了解你的应用中的更多内容.中心显示不同的内容类别,每个类别映射到应用 ...

  3. html5面向对象做一个贪吃蛇小游戏

    canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说 ...

  4. kettle的job

    1.首先创建一个job 2.拖拽组件形成下面的图 这里需要注意,在作业中的连线分为三类: 黄色锁的线:这个步骤执行之后,无论失败与否都会执行下一个步骤 绿色对号线:步骤执行成功了,才会执行下一个步骤. ...

  5. android任意view爆炸效果--第三方开源--ExplosionField

    犹如天女散花一样,爆炸散列,比较有趣.Android ExplosionField在github上的项目主页是:https://github.com/tyrantgit/ExplosionField ...

  6. js实现选项卡功能

    1.css .liclick{ border: 1px black solid; background: #fff; float: left; width: 80px; height: 35px; l ...

  7. Mongodb Gridfs

    http://www.cnblogs.com/lipan/archive/2011/03/21/1989409.html

  8. Oracle 11g 执行计划管理2

    1.创建测试数据 SQL> conn NC50/NC50 Connected. SQL)); SQL> insert into tab1 select rownum,object_name ...

  9. Python之路-(三级菜单)

    data = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youk ...

  10. Python学习教程(learning Python)--2.3.4Python函数返回值

    本节讨论Python函数返回值问题. Python和C语言一样,也可以在函数结束时返回一个值.但在定义自己的Python函数时,是不需要指定返回值数据类型的,这和Python不关心变量的数据类型是一致 ...