3.4.2 依赖与配置的细节

    3.4.2.1  Straight values (primitives, Strings, and so on)

    JavaBeans PropertyEditors被用来转换这些value到实际的类型。?

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="masterkaoli"/>
</bean>

使用p-namespace使得拼写更加简洁(可是拼写错误会在执行时才被发现,而不是在编辑时,除非使用Intellij IDEA或者SpringSource Tool Suite)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/mydb"
p:username="root"
p:password="masterkaoli"/> </beans>

第二种配置(这样的模式,Spring容器将element元素转成java.util.Properties实例通过使用JavaBeans PropertyEditor机制,在这样的配置下,Spring团队建议用这样的方法):

<bean id="mappings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>

关于idref元素,被用于<constructor-arg/>或者<property/>元素中,是一种防错措施。

<bean id="theTargetBean" class="..."/>

<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
<bean id="theTargetBean" class="..." />

<bean id="client" class="...">
<property name="targetName" value="theTargetBean" />
</bean>

以上第一种方案比另外一种方案好。理由:第一种配置能使得spring在部署的时候验证被引用的bean是否存在;另外一种则没有,仅仅有等这个bean被初始化的时候才会去验证。

此外,假设被引用的bean再同一个xml单元,并且bean的名字就是bean的id,那么能够使用local属性。这样一来同意xml解析器更早的去验证bean。

<property name="targetName">
<!-- a bean with id 'theTargetBean' must exist; otherwise an exception will be thrown -->
<idref local="theTargetBean"/>
</property>

3.4.2.2 对其它beans的引用

    ref元素作为<constructor-arg/>或者<property/>的一个属性。

    ref元素的bean属性能够跟相应bean的id属性一致,或者是相应bean的当中一个name一致。

    ref元素的local属性的值要跟相应bean的id属性一致。

    ref元素的parnet属性的值要跟相应bean的id属性一致,或者是相应bean的当中一个name一致。并且相应bean要存在于当前容器的父容器之中:

<!-- in the parent context -->
<bean id="accountService" class="com.foo.SimpleAccountService">
<!-- insert dependencies as required as here -->
</bean>
<!-- in the child (descendant) context -->
<bean id="accountService" <-- bean name is the same as the parent bean -->
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref parent="accountService"/> <!-- notice how we refer to the parent bean -->
</property>
<!-- insert other configuration and dependencies as required here -->
</bean>

3.4.2.3 内部beans

    <bean/>元素存在于<property/>或<constructor-arg/>元素中,就称之为内部bean。

<bean id="outer" class="...">
<!-- instead of using a reference to a target bean, simply define the target bean inline -->
<property name="target">
<bean class="com.example.Person"> <!-- this is the inner bean -->
<property name="name" value="Fiona Apple"/>
<property name="age" value="25"/>
</bean>
</property>
</bean>

内部bean不须要声明一个id或者name,容器会忽略掉这些属性。同一时候忽略掉scope属性,它的scope是prototypes。

3.4.2.4 集合

<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>

map的key或者value、set的value还能够是下面的不论什么一个元素:

bean | ref | idref | list | set | map | props | value | null

集合合并

    从spring2.0開始,就支持集合合并。

    集合合并同意定义父类型<list/>, <map/>, <set/>或 <props/>元素,然后利用子类型<list/>, <map/>, <set/>或 <props/>元素继承和重写父集合的值。终于子类型集合的值就是合并后的结果。

<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.com</prop>
<prop key="support">support@example.com</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the *child* collection definition -->
<props merge="true">
<prop key="sales">sales@example.com</prop>
<prop key="support">support@example.co.uk</prop>
</props>
</property>
</bean>
<beans>

上述结果:

administrator=administrator@example.com
sales=sales@example.com
support=support@example.co.uk

集合合并后对list会保存顺序,对set,map,properties则没有这一说法。

集合合并的约束:

    你不能使用不同类型的集合进行合并;

    merge属性仅仅能放在子类型的集合端;

    集合合并仅仅在spring2.0及以后的版本号提供。

强类型集合(spring在注入前,先推断accounts的属性,然后将要注入的值转成合适的类型注入):

public class Foo {

  private Map<String, Float> accounts;

  public void setAccounts(Map<String, Float> accounts) {
this.accounts = accounts;
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<property name="accounts">
<map>
<entry key="one" value="9.99"/>
<entry key="two" value="2.75"/>
<entry key="six" value="3.99"/>
</map>
</property>
</bean>
</beans>

3.4.2.5 null和空字符串

<bean class="ExampleBean">
<property name="email" value=""/>
</bean>

上述跟exampleBean.setEmail("")的效果是一样的;

<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>

上述跟 exampleBean.setEmail(null)的效果是一样的;

3.4.2.5 xml使用p-namespace作为快捷方式

    Spring2.0及之后的版本号支持。

    p-namespace并非被定义在xsd文件,而是仅仅存在于Spring的core。  

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="classic" class="com.example.ExampleBean">
<property name="email" value="foo@bar.com"/>
</bean> <bean name="p-namespace" class="com.example.ExampleBean"
p:email="foo@bar.com"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean> <bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/> <bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>
</beans>

3.4.2.7 Compound property names(引用对象的属性注入)

<bean id="foo" class="foo.Bar">
<property name="fred.bob.sammy" value="123" />
</bean>

叫foo的bean有一个fred属性,fred对象有一个bob属性,bob对象有一个sammy属性。要让这个配置有效,在foo被创建之后fred、bob、sammy属性不能够为null。否则报NullPointerException异常?

Spring3.0官网文档学习笔记(七)--3.4.2的更多相关文章

  1. Spring3.0官网文档学习笔记(一)

    Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...

  2. Spring3.0官网文档学习笔记(二)

    1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包)     spring-*.jar *号代表 ...

  3. Spring3.0官网文档学习笔记(四)--3.1~3.2.3

    3.1 Spring IoC容器与Beans简单介绍     BeanFactory接口提供对随意对象的配置:     ApplicationContext是BeanFactory的子接口.整合了Sp ...

  4. Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

    3.4.3 使用depends-on     使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...

  5. android官网文档学习笔记

    1.android的四大组件的了大概功能 activity:负责显示界面,和用户交互. service:运行在后台. content provider:为程序app之间的数据访问提供接口. broad ...

  6. Spring Security 官网文档学习

    文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...

  7. mongodb官网文档阅读笔记:与写性能相关的几个因素

    Indexes 和全部db一样,索引肯定都会引起写性能的下降,mongodb也没啥特别的,相对索引对读性能的提示,这些消耗通常是能够接受的,所以该加入的索引还是要加入.当然须要慎重一些.扯点远的,以前 ...

  8. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

  9. (二)Spring Boot 官网文档学习之入门

    文章目录 Spring Boot 是什么 系统要求 Servlet 容器 Maven方式安装Spring Boot 编写第一个 Spring Boot 项目 原文:https://docs.sprin ...

随机推荐

  1. LINQ to SQL的一些简单用法

    static void Main(string[] args) { var personList = new List<Person> { new Person() { PersonID= ...

  2. 搭建Go开发及调试环境(LiteIDE + GoClipse)

    搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇 这里以Windows7 64位为例,如果是32位环境需安装对应版本程序. 一.安装golang1.2.2 1.3 ...

  3. [转载]关于网传JDK1.7语法层次支持集合的问题

    以  JDK1.7新特性 为关键词进行百度的话,总能发现这样的描述,说: 从语法层面上支持集合,不再是数组的专利.还有这样的例子: final List<Integer> piDigits ...

  4. uvc摄像头代码解析6

    10.扫描视频设备链和注册视频设备 10.1 uvc视频链 struct uvc_video_chain { //uvc视频链 struct uvc_device *dev; //uvc设备 stru ...

  5. linux 控制台使用技巧

    1. 键盘无响应 可能是按下ctrl+s, 此时按下scroll即可解锁 2. 想看上一屏的信息 shift+pageup 3. 打印的信息和错误信息区分 普通信息, 用printf, cout打印的 ...

  6. [置顶] 手把手教你iOS消息推送证书生成以及Push消息

    iOS推送消息是许多iOS应用都具备的功能,今天在给应用加推送功能,在生成证书的过程中,发生了各种令人蛋痛的事.下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provi ...

  7. zabbix 获取jvm session信息

    zabbix:/root# java -jar /root/cmdline-jmxclient-0.10.3.jar - 121x:5566 "Catalina:type=Manager,c ...

  8. 消息函数一般是私有的,因为不需要程序员显示的调用,但子类如果需要改写这个方法,则改成保护方法Protected

    许多的面向对象程序设计语言都支持对消息的处理.消息处理是一种动态响应客户类发出的请求,它与过程调用不同.过程调用中,客户类必须知道服务类提供了哪些过程,以及每个过程的调用约定,并且在调用时需要明确指出 ...

  9. Linux内核参数信息(Oracle相关)

    命令行:vim  /etc/sysctl.conf 查看如下两行的设置值,这里是: kernel.shmall = 2097152 kernel.shmmax = 4294967295 如果系统默认的 ...

  10. [Java 8] (6) Lambda与资源管理

    资源处理 Java本身自带了垃圾回收(Garbage Collection)功能.可是仅仅有垃圾回收的目标是内部资源(Internal Resource),典型的比方堆上分配的内存区域等.对于外部资源 ...