Spring框架中<constructor-arg>与<property>理解
配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class。id表示组件的默认名称,class表示组件的类型。
- 依赖注入的方式: constructor-arg:通过构造函数注入。 property:通过setxx方法注入。
- 设值注入,使用property子标签:
<bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer"> <property name="messageProvider">
<ref local="provider"/>
</property>
</bean>
- 构造子注入,使用constructor-arg子标签:
<bean id="provider"class="com.apress.prospring.ch4.ConfigurableMessageProvider"> <constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
- 使用constructor-arg标签的index属性,对应于构造函数的多个参数,index属性的值从0开始。
<bean id="provider" lass="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>
- 使用constructor-arg的type属性,避免构造子冲突:
<bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">
<constructor-arg type="int">
<value>90</value>
</constructor-arg>
</bean>
public class ConstructorConfusion {
public ConstructorConfusion(String someValue) {
System.out.println("ConstructorConfusion(String) called");
}
public ConstructorConfusion(int someValue) {
System.out.println("ConstructorConfusion(int) called");
}
}
注入集合属性,使用list,map,set和props标签,分别对应List,Map,Set和Properties:
<bean id="injectCollection" class="com.apress.prospring.ch4.CollectionInjection">
<property name="map">
<map>
<entry key="someValue">
<value>Hello World!</value>
</entry>
<entry key="someBean">
<ref local="oracle"/>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="firstName">
Rob
</prop>
<prop key="secondName">
Harrop
</prop>
</props>
</property>
<property name="set">
<set>
<value>Hello World!</value>
<ref local="oracle"/>
</set>
</property>
<property name="list">
<list>
<value>Hello World!</value>
<ref local="oracle"/>
</list>
</property>
</bean>
Spring框架中<constructor-arg>与<property>理解的更多相关文章
- Spring5源码解析-Spring框架中的单例和原型bean
Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...
- Spring框架中文件目录遍历漏洞 Directory traversal in Spring framework
官方给出的描述是Spring框架中报告了一个与静态资源处理相关的目录遍历漏洞.某些URL在使用前未正确加密,使得攻击者能够获取文件系统上的任何文件,这些文件也可用于运行SpringWeb应用程序的进程 ...
- 再析在spring框架中解决多数据源的问题
在前面我写了<如何在spring框架中解决多数据源的问题>,通过设计模式中的Decorator模式在spring框架中解决多数据源的问题,得到了许多网友的关注.在与网友探讨该问题的过程中, ...
- Spring框架中 配置c3p0连接池 完成对数据库的访问
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- Spring框架中的定时器 使用和配置
Spring框架中的定时器 如何使用和配置 转载自:<Spring框架中的定时器 如何使用和配置>https://www.cnblogs.com/longqingyang/p/554543 ...
- 细说shiro之五:在spring框架中集成shiro
官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
- Spring框架中 配置c3p0连接池
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- Spring框架中ModelAndView、Model、ModelMap区别
原文地址:http://www.cnblogs.com/google4y/p/3421017.html SPRING框架中ModelAndView.Model.ModelMap区别 注意:如果方法 ...
- 【Spring】8、Spring框架中的单例Beans是线程安全的么
看到这样一个问题:spring框架中的单例Beans是线程安全的么? Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上, ...
随机推荐
- RSA加密解密算法
/** * RSA加密解密算法 * Class Rsa */ class Rsa { /** * 获取pem格式的公钥 * @param $public_key 公钥文件路径或者字符串 * @retu ...
- Linux从入门到入门
一. 前言 首先,在你的Windows系统上要想有linux系统,那就必须先安装一款软件,这里提供的是14.15的,还有ISO镜像:VMware-workstation 安装VMware:略 新建虚拟 ...
- STM32F0使用LL库实现PWM输出
在本次项目中,限于空间要求我们选用了STM32F030F4作为控制芯片.这款MCU不但封装紧凑,而且自带的Flash空间也非常有限,所以我们选择了LL库实现.本文我们将说明如何通过LL库实现PWM信号 ...
- 2018-2019-2 20165314《网络对抗技术》Exp1 PC平台逆向破解
实践目的 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShel ...
- lombok @Accessors用法
@Accessors 翻译是存取器.通过该注解可以控制getter和setter方法的形式. fluent 若为true,则getter和setter方法的方法名都是属性名,且setter方法返回当前 ...
- 通过GIT_COMMIT进行代码回滚
首先需要安装插件:conditional-buildstep A buildstep wrapping any number of other buildsteps, controlling thei ...
- 论文阅读笔记五十二:CornerNet-Lite: Efficient Keypoint Based Object Detection(CVPR2019)
论文原址:https://arxiv.org/pdf/1904.08900.pdf github:https://github.com/princeton-vl/CornerNet-Lite 摘要 基 ...
- Redis 学习目录
Redis in .NET Core 入门 Redis实战 - 1.String和计数器 Redis实战 - 2.list.set和Sorted Set Redis实战 - 4.Key Redis实战 ...
- 恢复数据库的redo日志文件(由于异常关机引起)
需要事先进入sqlplus select * from v$log 打印相关文件信息 1.recover database until cancel(recover database using ba ...
- vue v-if 和 v-show 的知识点
1.v-if 的特点: 实现方式:根据后面数据的真假判断是否重新删除或创建元素. 性能消耗:有较高的切换性能消耗. 编译过程:v-if 切换有一个局部编译/卸载的过程,切换过程中合适地销毁和重建内部的 ...