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) 和 ...
随机推荐
- LeetCode_Merge Two Sorted Lists
一.题目 Merge Two Sorted Lists My Submissions Merge two sorted linked lists and return it as a new list ...
- html5css3杂记
最新版本号的safari.chrome.firefox以及opera支持某些html5特性.ie9将支持某些html5特性. html5提供了展现视频的标准<video>支持ogg及mpe ...
- linux它SQL声明简明教程---WHERE
我们并不一定必须注意,每次格里面的信息是完全陷入了.在很多情况下,我们需要有选择性地捕捞数据.对于我们的样本.我们可以只抓住一个营业额超过 $1,000 轮廓. 做这个事情,我们就须要用到 WHERE ...
- HDU 4815 背包
标题的含义给出N问题.和概率P,然后给予相应的分数为每个问题x(每个问题只有两种选择,纠正错误). 两个人来回答.一个人是随机选择的答案,问:还有一个人的至少一些点的能力有保证P概率不会失败. 01背 ...
- MD5加密算法的实现
//////////////////////////////////////////////////////////////////// /* md5.h ...
- Ajax动态载入xml文件内容
<%@page import="javax.swing.JOptionPane"%> <%@page import="com.ctl.util.*&qu ...
- MySQL外键约束OnDelete和OnUpdate的使用
On Delete和On Update都有Restrict,No Action, Cascade,Set Null属性.现在分别对他们的属性含义做个解释. ON DELETE restrict(约束) ...
- jar包有嵌套的jar的打包成jar的方法
1.先写一个类,将其打包成jar包. 代码如下: package com.wjy.jar; public class GetUserName { public String getUserName() ...
- 让CentOS系统时间同步
CentOS系统时间同步的步骤如下: 新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步cp /usr/share/zone ...
- Eclipse 快捷键整理
Alt+/:代码提示Ctrl+/:注释/取消注释Ctrl+D:删除光标所在行Ctrl+K:将光标停留在变量上,按Ctrl+K键可以查找到下一个同样的变量Shift+Ctrl+K:和Ctrl+K查找的方 ...