配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class。id表示组件的默认名称,class表示组件的类型。

  1. 依赖注入的方式:  constructor-arg:通过构造函数注入。   property:通过setxx方法注入。
  2.  设值注入,使用property子标签:

    <bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer">                       <property name="messageProvider">

    <ref local="provider"/>

    </property>

    </bean>

  3.  构造子注入,使用constructor-arg子标签:

    <bean id="provider"class="com.apress.prospring.ch4.ConfigurableMessageProvider">       <constructor-arg>

    <value>This is a configurable message</value>

    </constructor-arg>

    </bean>

  4.  使用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>

  5. 使用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");

    }

    }

  6.  

    注入集合属性,使用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>理解的更多相关文章

  1. Spring5源码解析-Spring框架中的单例和原型bean

    Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...

  2. Spring框架中文件目录遍历漏洞 Directory traversal in Spring framework

    官方给出的描述是Spring框架中报告了一个与静态资源处理相关的目录遍历漏洞.某些URL在使用前未正确加密,使得攻击者能够获取文件系统上的任何文件,这些文件也可用于运行SpringWeb应用程序的进程 ...

  3. 再析在spring框架中解决多数据源的问题

    在前面我写了<如何在spring框架中解决多数据源的问题>,通过设计模式中的Decorator模式在spring框架中解决多数据源的问题,得到了许多网友的关注.在与网友探讨该问题的过程中, ...

  4. Spring框架中 配置c3p0连接池 完成对数据库的访问

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  5. Spring框架中的定时器 使用和配置

    Spring框架中的定时器 如何使用和配置 转载自:<Spring框架中的定时器 如何使用和配置>https://www.cnblogs.com/longqingyang/p/554543 ...

  6. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

  7. Spring框架中IoC(控制反转)的原理(转)

    原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...

  8. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  9. Spring框架中ModelAndView、Model、ModelMap区别

    原文地址:http://www.cnblogs.com/google4y/p/3421017.html SPRING框架中ModelAndView.Model.ModelMap区别   注意:如果方法 ...

  10. 【Spring】8、Spring框架中的单例Beans是线程安全的么

    看到这样一个问题:spring框架中的单例Beans是线程安全的么? Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上, ...

随机推荐

  1. 手动安装sublime text3 文本编辑器是控制台

    1 本来想安装一个体积小.功能又强大的文本编辑器,百度了一圈sublime text3 的口碑不错,然后就安装试试吧, 下了以后安装成功后,看到介绍sublime text3功能强大是因为他可以安装多 ...

  2. 自己对Web标准的理解

    1.WEB标准 WEB分层: 1.结构层(HTML)   2.表现(css) 3.行为(js) web标准的优点: * 易于维护:只需更改css文件,就能改变整站的样式: * 页面响应快:HTML文档 ...

  3. 二丶Django~1

      一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于 ...

  4. 四 Memcache

    官网http://memcached.org 了解 cookies和session 安装memcached # yum install memcached  -y 安装nc和telnet 来管理mem ...

  5. Jace 上新建 Station 配置 笔记

    1.Station站点的结构图 2.niagara 结构框架图 Niagara 系统的架构是围绕着“以组件(Component)为导向的编程”为核心设计的.组件(Component)是使用Java 编 ...

  6. mysql 与 oracle 的时间查询

    关于时间区间查询 1.mysql select * from t_date a where date_format (a.delete_time,'%Y-%m-%d') <date_format ...

  7. javascript中new Date()存在的兼容性问题

    问题:通过new Date()创建的时间对象在Chrome能正常工作,但在IE浏览器却显示NaN 代码: var time = new Date(date + ' 00:00:00'); //NaN ...

  8. [原创]SecureCRT终端软件连接VMware Workstation Pro虚拟机

    Step1:检查主机的桥接有没有禁用 Step2:进入Ubuntu系统,进入到Ubuntu下,先查看Ubuntu虚拟机的IP配置,打开终端(Ctrl+Alt+T),通过ifconfig命令查看,可以看 ...

  9. React知识杂烩(持续更新)

    每隔半年不看官方文档,你就会不认识React了

  10. xcode升级10

    ld: library not found for -lstdc++.6 删除 libstdc++ stackoverflow Building with libstdc++ was deprecat ...