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) 和 ...
随机推荐
- 安卓MP3播放器开发实例(1)之音乐列表界面
学习安卓开发有一年了,想想这一年的努力,确实也收获了不少.也找到了比較如意的工作. 今天准备分享一个以前在初学阶段练习的一个项目.通过这个项目我真正的找到了开发安卓软件的感觉,从此逐渐步入安卓开发的正 ...
- Linux 技巧之 Grub 超实用技巧
1. 简单介绍 什么是 GRUB?GRUB 全名Grand Unified Boot Loader,它是一个引导装入器 -- 它负责装入内核并引导 Linux 系统.GRUB 还能够引导其他操作系统, ...
- RIA Test:try catch 对 Error #1009 (无法访问空对象引用的属性或方法)的处理
功能: 实现登录账户的强制登录, 用If 判断当前账户是否可用.若可用,则跳出if体直接登录,若不可用,则进入If体点击 “强制登录” 按钮. 问题:如果不可用,则if 条件中的对象不可见,这样程序会 ...
- Uva - 11419 - SAM I AM
题意:一个矩形——R*C的网格,在某些位置上有石头,在网格外开一炮可以打掉该行或者该列的石头,求打掉这些石头最少需要多少门大炮,位置分别设在哪行哪列(0<R<1001, 0 < C ...
- DatePicker的使用
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- ExtJs 设置GridPanel表格文本垂直居中
业务场景,需要实现最终效果图如下: GridPanel代码如下配置: { xtype : 'grid', id : 'grid_jglb', frame : true, region : 'cente ...
- WPF案例 (四) 模拟Windows7桌面任务栏
原文:WPF案例 (四) 模拟Windows7桌面任务栏 这个程序模彷了Windows7的桌面任务栏,当在桌面上双击某个快捷方式时,将打开一个新的子界面,并且在任务栏里创建一个链接到此界面的任务栏图标 ...
- poj1679(最小生成树)
传送门:The Unique MST 题意:判断最小生成树是否唯一. 分析:先求出原图的最小生成树,然后枚举删掉最小生成树的边,重做kruskal,看新的值和原值是否一样,一样的话最小生成树不唯一. ...
- poj1182(并查集)
题目链接 分析:根据分析,关系的递推满足由[a,b]~[b,c]得:[a,c]=([a,b]+[b,c])%3;[a,d]=([a,b]+[b,c]+[c,d])%3.由rank数组表示关系 0 - ...
- c#与oracle数据库连接池
c#与oracle数据库连接池 在做一个项目,中间要使用webservice和oracle数据库.我在服务端做了用户身份认证,也就是使用session传递用户的登陆信息.在测试时,当用户少的时候,没有 ...