3、Spring4之Bean 配置的细节
value 节点的 <![CDATA[]]> 把字面值包裹起来。
<constructor-arg>
<!-- 若 value 属性值中包括特殊字符串, 则能够使用 value 子节点来注入属性值. value 子节点中能够使用 CDATA -->
<value><![CDATA[Zheng <><> zhou]]></value>
</constructor-arg>
2). 在 Bean 的配置文件里, 能够通过 <ref> 元素或 ref 属性为
Bean 的属性或构造器參数指定对 Bean 的引用.
<bean id="dao" class="com.atguigu.spring.ioc.ref.Dao">
<property name="database" value="DB2"></property>
</bean>
<bean id="service2" class="com.atguigu.spring.ioc.ref.Service">
<property name="dao" ref="dao"></property>
</bean>
------------------------------------------------------------------------------
解析:<property name="dao" ref="dao"></property> 的作用为:
Dao dao = (Dao)ctx.getBean("dao");
Service service = (Service)ctx.getBean("service2");
service.setDao(dao);
3). 能够在属性或构造器里包括 Bean 的声明, 这种 Bean 称为内部 Bean
<bean class="com.atguigu.spring.ioc.ref.Service" id="service">
<property name="dao">
<bean class="com.atguigu.spring.ioc.ref.Dao">
<property name="database" value="MySQL"></property>
</bean>
</property>
</bean>
解释:类似于下面代码,但 dao 的这个 bean 事实上是没有 id 的,也不能被其它的 bean 来引用。也不能单独从 IOC 容器中获取。
<bean class="com.atguigu.spring.ioc.ref.Dao" id="dao">
<property name="database" value="MySQL"></property>
</bean>
<bean class="com.atguigu.spring.ioc.ref.Service" id="service">
<property name="dao" ref="dao"></property>
</bean>
①. 当 Bean 实例只给一个特定的属性使用时, 能够将其声明为内部 Bean.
内部 Bean 声明直接包括在 <property> 或 <constructor-arg> 元素里, 不须要设置不论什么 id 或 name 属性
②. 内部 Bean 不能使用在不论什么其它地方
4). 能够使用专用的 <null/> 元素标签为 Bean 的字符串或其他对象类型的属性注入
null 值
<bean id="car6" class="com.atguigu.spring.ioc.Car">
<!-- 为 maxSpeed 赋值为 null, 而 value="null" 是把 null 这个字符串赋给了相应的属性 -->
<property name="corp"><null/></property>
</bean>
5). Spring 支持级联属性的配置。
<bean id="action3" class="com.atguigu.spring.ioc.ref.Action">
<property name="service" ref="service2"></property>
<!-- 为 service 的 dao 的 database 赋值为 ORACLE -->
<property name="service.dao.database" value="ORACLE"></property>
</bean>
6). 配置 java.util.List 类型的属性, 须要指定 <list> 标签, 在标签里包括一些元素.
这些标签能够通过 <value> 指定简单的常量值, 通过 <ref> 指定对其它 Bean 的引用.
通过<bean> 指定内置 Bean 定义.
通过 <null/> 指定空元素. 甚至能够内嵌其它集合.
<property name="cars">
<!-- 通过 list 指定集合属性的值. 但 list 是一个内部 list, 不能被其它的 bean 引用. -->
<list>
<!-- ref 直接指向已有的 bean -->
<ref bean="car"/>
<ref bean="car2"/>
<ref bean="car3"/>
<!-- bean 声明内部 bean -->
<bean class="com.atguigu.spring.ioc.Car">
<property name="brand" value="BMW"></property>
<property name="corp" value="HuaChen"></property>
<property name="maxSpeed" value="300"></property>
<property name="price" value="800000"></property>
</bean>
</list>
</property>
7). 能够使用 util schema 里的集合标签定义独立的集合 Bean. 须要注意的是, 必须在 <beans> 根元素里加入 util schema 定义
①. 导入 util 命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
②. 定义集合 Bean
<util:list id="cars">
<!-- ref 直接指向已有的 bean -->
<ref bean="car"/>
<ref bean="car2"/>
<ref bean="car3"/>
<!-- bean 声明内部 bean -->
<bean class="com.atguigu.spring.ioc.Car">
<property name="brand" value="BMW"></property>
<property name="corp" value="HuaChen"></property>
<property name="maxSpeed" value="300"></property>
<property name="price" value="800000"></property>
</bean>
</util:list>
8). 配置 Map Bean
<util:map id="config">
<entry key="user" value="root"></entry>
<entry key="password" value="1230"></entry>
<entry key="driverClass" value="com.mysql.jdbc.Driver"></entry>
<entry key="jdbcUrl" value="jdbc:mysql:///test"></entry>
</util:map>
9). 配置 Properties 的属性
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="b">B</prop>
<prop key="c">C</prop>
<prop key="d">D</prop>
</props>
</property>
10). 使用 p 命名空间简化 bean 的属性配置:
①. 导入 p 的命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
②. 使用 p 命名空间进行配置
<bean id="car7" class="com.atguigu.spring.ioc.Car"
p:brand="Mazda"
p:corp="ChangAn"
p:maxSpeed="220"
p:price="200000"/>
3、Spring4之Bean 配置的细节的更多相关文章
- 2 Spring4 之Bean的配置
Spring4 之Bean的配置 1 IOC & DI 概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源 ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 丢弃重口味的xml配置--spring4用groovy配置bean(转)
spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息.另一种方式,是从spring3.0开始, ...
- Spring4学习笔记二:Bean配置与注入相关
一:Bean的配置形式 基于XML配置:在src目录下创建 applicationContext.xml 文件,在其中进行配置. 基于注解配置:在创建bean类时,通过注解来注入内容.(这个不好,因 ...
- Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...
- 关于spring中bean配置的几件小事
一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring Bean配置2
Spring表达式语言:SpEL •Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框 ...
- Spring Bean配置
Spring 是什么 •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. •Spring 是一个 IOC(DI) 和 ...
随机推荐
- Maven聚合
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2 ...
- C#使用Redis集群缓存
C#使用Redis集群缓存 本文介绍系统缓存组件,采用NOSQL之Redis作为系统缓存层. 一.背景 系统考虑到高并发的使用场景.对于并发提交场景,通过上一章节介绍的RabbitMQ组件解决.对于系 ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- Activity数据传输到服务
activity数据接口负责启动该服务包.service获取数据.手术. 详细demo如下面: package com.example.android_service_trance; import a ...
- Andorid Clip 实现自定义的进度条效果实例
Android该系统提供了一个水平进度条为我们展现了运行使用进展情况,水平进度条显示用于运行进度Clip Drawable技术 下面我们通过一个具体的例子来说明Clip Drawable使用. 还有我 ...
- 【WinRT】【译】【加工】在 XAML 中制作圆形图片
原文:[WinRT][译][加工]在 XAML 中制作圆形图片 原文地址:http://timheuer.com/blog/archive/2015/05/06/making-circular-ima ...
- Extjs4 RowEditing 的使用和更新方法
如何灵活快速的掌握RowEditing组件的应用,应大家的要求,今天给大家具体讲下该组件的使用. 1.创建 var rowEditing = Ext.create('Ext.grid.plugin.R ...
- 跟我extjs5(38--单个模块的设计[6获得模块列表数据])
跟我extjs5(38--单个模块的设计[6获得模块列表数据]) 在程序的前一个表以及,据的执行过程. 在菜单中选择 "系统管理"--"模块分组" ...
- ssl https双向验证的配置与证书库的生成
1.SSL认证 不须要特别配置,相关证书库生成看https认证中的相关部分 2.HTTPS认证 一.基本概念 1.单向认证,就是传输的数据加密过了,可是不会校验client的来源 2.双向认证,假设 ...
- 不可不知的DIP、IoC、DI以及IoC容器
面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.当中.OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念. 本文首先用实例阐述四个概 ...