spring - constructor-arg的常规用法
1、使用构造子注入时,则使用constructor-arg子标签,来指定构造函数的参数。
<bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">
<constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
2、当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。
<bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">
<constructor-arg index="0">
<value>first parameter</value>
</constructor-arg>
<constructor-arg index="1">
<value>second parameter</value>
</constructor-arg>
</bean>
3、 在使用构造子注入时,需要注意的问题是要避免构造子冲突的情况发生。考虑下面的情况:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class ConstructorConfusion { public ConstructorConfusion(String someValue) { System.out.println( "ConstructorConfusion(String) called" ); } public ConstructorConfusion( int someValue) { System.out.println( "ConstructorConfusion(int) called" ); } } |
使用如下配置文件
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">
<constructor-arg>
<value>90</value>
</constructor-arg>
</bean>
那 么,当实例化组件constructorConfusion时,将输出ConstructorConfusion(String) called,也就是 说参数类型为String的构造函数被调用了,这显然不符合我们的要求。为了让Spring调用参数为int的构造函数来实例化组件 constructorConfusion,我们需要在配置文件中明确的告诉Spring,需要使用哪个构造函数,这需要使用constructor- arg的type属性。
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">
<constructor-arg type="int">
<value>90</value>
</constructor-arg>
</bean>
spring - constructor-arg的常规用法的更多相关文章
- GridView的常规用法
GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔用到的,查找.使用.记录,仅此而已.(最后附带DropDownList控件) ASP.NET中GridView ...
- mapreduce的cleanUp和setUp的特殊用法(TopN问题)和常规用法
一:特殊用法 我们上来不讲普通用法,普通用法放到最后.我们来谈一谈特殊用法,了解这一用法,让你的mapreduce编程能力提高一个档次,毫不夸张!!!扯淡了,让我们进入正题: 我们知道reduce和m ...
- Spring AOP 中@Pointcut的用法
Spring Aop中@pointCut的用法,格式:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? nam ...
- 【python】-matplotlib.pylab常规用法
目的: 了解matplotlib.pylab常规用法 示例 import matplotlib.pylab as pl x = range(10) y = [i * i for i in x] pl. ...
- spring 中配置sessionFactory及用法
spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入 --> ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- MarkDown的常规用法
MarkDown的常规用法 标题 # 一级标题 ## 二级标题 ... ###### 六级标题 列表 第二级 - 和 空格 + 和 空额 * 和 空格 第三级 代码块 多行代码块 3个` 回车 单行代 ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- 关于Spring定时任务(定时器)用法
Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...
随机推荐
- 廖雪峰Java11多线程编程-1线程的概念-3线程的状态
1线程的状态 线程终止的的原因: run()或call()方法执行完成,线程正常结束 线程抛出一个未捕获的Exception或Error 直接调用该线程的stop()方法来结束该线程--该方法容易导致 ...
- linux服务器项目搭建常用命令
linux下载链接文件 wget -c 后面是该网络地址和文件的位置. 例如:wget -c http://apache.opncas.or/MySQL/MySQL-7/v7.0.67/bin/MyS ...
- ArcGIS Data Interoperability 的使用(1)
今天在用OneMap的时候,发现OneMap中注册过后的WFS服务无法在skyline中加载,于是想知道OneMap注册后的WFS服务与server中的原生态WFS服务有啥区别.首先想到是否能在Arc ...
- Java虚拟机原理图解-- 1.1、class文件基本组织结构 [转]
作为Java程序猿,我们知道,我们写好的.java 源代码,最后会被Java编译器编译成后缀为.class的文件,该类型的文件是由字节组成的文件,又叫字节码文件.那么,class字节码文件里面到底是有 ...
- Android App的设计架构:MVC,MVP,MVVM与架构AAAAA
1. 架构设计的目的1.1 通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.1.2 这样做的好处是使得程序在开发的过程中,开发人员只需要专注于一点,提高程序开发的效率,并且更容易进行后续 ...
- 原生JS实现动态时钟(优化)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- TZOJ 4292 Count the Trees(树hash)
描述 A binary tree is a tree data structure in which each node has at most two child nodes, usually di ...
- Blow up the city
Blow up the city 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Country A and B are at war. Country A needs to organ ...
- MySQL加快批量更新 UPDATE优化
如果是更新为同样的内容,没啥难度,直接在where里面下功夫就好了,大家都懂,我要说的是针对更新内容不一样的情况 首先,先看看网上转载的方法: mysql 批量更新如果一条条去更新效率是相当的慢, 循 ...
- LR11中web_save_timestamp_param
时间戳是现在时间减去现在的时间 减去 1970年1月1日0点00 的时间 ,然后换算成毫秒. 所以我们需要借助 web_save_timestamp_param 来实现. web_save_times ...