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) 和 ...
随机推荐
- dsp下基于双循环缓冲队列的视频采集和显示记录
对最近在设计的视频采集和显示缓冲机制做一个记录,以便以后使用. 视频采集和显示缓冲机制,其实是参考了Linux下v4L2的驱动机制,其采用输入多缓冲frame,输出多缓冲的切换机制.简单的就是ping ...
- BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 521 Solved: 159 [ Subm ...
- hdu 1086(判断线段相交)
传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...
- android 在你的UI中显示Bitmap - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Displaying Bitmaps in Your UI 在你的UI中显示Bitmap ...
- 登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错
登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错 [oracle@localhost admin]$ date Tue ...
- Andorid Clip 实现自定义的进度条效果实例
Android该系统提供了一个水平进度条为我们展现了运行使用进展情况,水平进度条显示用于运行进度Clip Drawable技术 下面我们通过一个具体的例子来说明Clip Drawable使用. 还有我 ...
- dedecms 文章列表和频道列表同时调用
演示效果:http://www.mypf110.com/qcd/ <div class="changshi_wrap"> {dede:channelartlist ro ...
- selenium让人摸不着头脑的问题
selenium让人摸不着头脑的问题 问题一 使用webdriver驱动firefox浏览器时如果不设置参数,默认使用的Firefox的profile和平时打开浏览器使用的firefox不一样,如果要 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- red hat Linux 使用CentOS yum源更新
red hat linux是商业版软件,没有经过注册是无法使用红帽 yum源更新软件的,使用CentOS源更新操作如下: 1.删除red hat linux 原有的yum 源 rpm -aq | gr ...