在Spring中,对象无需自己负责查找或创建与其关联的其他对象。相反,容器负责把需要相互协作的对象引用赋予各个对象。创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质。

1.声明Bean

1.1 创建Spring配置

在XML文件中声明Bean时,Spring配置文件的根元素是来源于Spring beans命名空间所定义的<beans>元素。以下是一个典型的Spring XML配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
  4. xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  12. http://www.springframework.org/schema/jee
  13. http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  16. http://www.springframework.org/schema/lang
  17. http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util-3.1.xsd">
  20.  
  21. <!-- Bean declaration go here -->
  22.  
  23. </beans>

在<beans>元素内,可以放置所有的Spring配置信息,包括<bean>元素的声明。

Spring核心框架自带的10个命名空间配置如下:

1.2 声明一个简单的Bean

    <bean>元素是Spring中最基本的配置单元,通过该元素Spring将创建一个对象。Spring使用反射来创建Bean。

1.3 通过构造器注入

     1) 通过构造器注入对象引用
  1. <bean id="zookeeperSources" class="com.dangdang.config.service.easyzk.support.spring.ZookeeperSourceFactoryProxy" >
  2. <constructor-arg name="appName" value="${server.name}" />
  3. <constructor-arg name="configFactory" ref="configFactory" />
  4. <constructor-arg name="nodes" value=""/>
  5. </bean>

2) 通过工厂化方法创建Bean

  1. <bean id="zookeeperSources" class="com.dangdang.config.service.ZookeeperSource" factory-method="create"/>

1.4 Bean的作用域

    所有的Spring Bean默认都是单例。当容器分配一个Bean时,它总返回Bean的同一个实例。为了让Spring在每次请求时都为Bean产生一个新的实例,我们只需配置Bean的scope属性为prototype。
  1. <bean id="zookeeperSources" class="com.dangdang.config.service.ZookeeperSource" scope="prototype"/>

Spring的作用域选项:

1.5 初始化和销毁Bean

    为Bean定义初始化和销毁操作,只需使用init-method和destroy-method参数来配置<bean>元素。init-method属性指定了在初始化Bean时要调用的方法,destroy-method属性指定了Bean从容器移除前要调用的方法。
  1. <bean id="auditor" class="com.springinaction.springidol.Auditor" init-method="turnOnLights" destroy-method="turnOffLights" />

可以使用<beans>元素的default-init-method和default-destroy-method为应用上下文中所有的Bean设置共同的初始化和销毁方法。

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="
  3. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-init-method="
  4. turnOnLights" default-destroy-method="turnOffLights">
  5. </beans>

2.注入Bean属性

2.1注入简单值

  1. <bean id =“kenny" class="com.ouc.springinaction.test">
  2. <property name ="song" value="Bells" />
  3. </bean>
  一旦test被实例化,Spring就会调用<property>元素所指定属性的setter方法为该属性注入值。

2.2 引用其他Bean

  1. <bean id =“saxon" class="com.ouc.springinaction.saxontest"></bean>
  2. <bean id =“kenny" class="com.ouc.springinaction.test2">
  3. <property name ="song" value="Bells" />
  4. <property name ="saxonIns" ref="saxon" />
  5. </bean>

Spring提倡面向接口编程,面向接口编程与依赖注入协作实现了松散耦合。

2.3 注入内部Bean

  1. <bean id =“saxon" class="com.ouc.springinaction.saxontest"></bean>
  2. <bean id =“kenny" class="com.ouc.springinaction.test2">
  3. <property name ="song" value="Bells" />
  4. <property name ="saxonIns">
  5. <bean class="com.ouc.springinaction.saxontest">
  6. /property>
  7. </bean>

内部Bean是通过直接声明一个<bean>元素作为<property>元素的子节点而定义的。

   内部Bean没有ID属性,使用内部Bean的最大缺点:不能被复用,内部Bean仅适用于一次注入,而且不能被其他Bean所引用。

2.4 使用Spring的命名空间p装配属性

  命名空间p的schema URI为http://www.springframework.org/schema/p.
  1. <bean id =“kenny" class="com.ouc.springinaction.test3">
  2. p:song = "Bells"
  3. p:saxonIns-ref = "saxon" />
  4. </bean>

使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。-ref后缀作为一个标识来告知Spring应该装配一个引用。

2.5 装配集合

 Spring提供了4种类型的集合配置元素。
   <props>要求键和值都必须为String类型,而<map>允许键和值可以是任意类型

  1. <bean id =“hank" class="com.ouc.springinaction.test4">
  2. <property name ="ins">
  3. <set>
  4. <ref bean = "guitar" />
  5. <ref bean = "cymbal" />
  6. <ref bean = "harmonica" />
  7. <ref bean = "harmonica" />
  8. </set>
  9. </property>
  10. </bean>
  11. <bean id =“hank" class="com.ouc.springinaction.test4">
  12. <property name ="ins">
  13. <list>
  14. <ref bean = "guitar" />
  15. <ref bean = "cymbal" />
  16. <ref bean = "harmonica" />
  17. </list>
  18. </property>
  19. </bean>

装配Map集合

  1. <bean id =“hank" class="com.ouc.springinaction.test4">
  2. <property name ="ins">
  3. <map>
  4. <entry key="GUITAR" value-ref = "guitar" />
  5. <entry key="GUITAR1" value-ref = "guitar1" />
  6. <entry key="GUITAR2" value-ref = "guitar2" />
  7. </map>
  8. </property>
  9. </bean>

    装配Properties集合
  1. <bean id =“hank" class="com.ouc.springinaction.test4">
  2. <property name ="ins">
  3. <props>
  4. <prop key="GUITAR"> STRUM </prop>
  5. <prop key="CYMBAL"> CRASH </prop>
  6. </props>
  7. </property>
  8. </bean>

● <property> 元素用于把值或Bean引用注入到Bean的属性中。
   ● <props> 元素用于定义一个java.util.Properties类型的集合值。

   ● <prop> 元素用于定义<props>集合的一个成员。
   装配空值
   为了覆盖自动装配的值或不能完全确定属性的值是否为null,此时,必须显示地为该属性装配一个null值。

  1. <property name ="nullIns"><null/></property>

2.6 使用表达式装配

  Spring表达式语言(SpEL)拥有许多特性,包括:
  ● 使用Bean的ID来引用Bean;
  ● 调用方法和访问对象的属性;
  ● 对值进行算术、关系和逻辑运算;
  ● 正则表达式匹配;
  ● 集合操作。
 字面值
  1. <property name="cappacity" value="#{1e4}" />
 引用Bean、Properties和方
  1. <property name="ins" value="#{saxno.song}" />
  2. <property name="ins" value="#{saxno.song()?.toUpperCase()}" />

使用?.运算符代替点(.)来访问toUpperCase()方法。
  操作类

  使用T()运算符会调用类作用域的方法和常量,通过该运算符可以访问指定类的静态方法和常量。

  1. <property name="mathplier" value="#{T(java.lang.Math).PI}" />
  2. <property name="randomNum" value="#{T(java.lang.Math).random()}" />

使用SpEL进行数值运算

  1. <property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}" />

比较值

  1. <property name="hasCap" value="#{count.total le 10000}" />

逻辑表达式

  1. <property name="outStock" value="#{!pro.available}" />
  2. <property name="outStock" value="#{not pro.available}" />

  条件表达式

  1. <property name="song" value="#{keny.song != null ? keny.song : 'Green'}" />
  2. <property name="song" value="#{keny.song ?: 'Green'}" />

SpEL正则表达式

  1. <property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}" />

使用<util:list>元素在Spring里配置一个包含City对象的List集合。

  1. <util:list id ="cities">
  2. <bean class="com.habuma.spel.city"
  3. p:name="Chicago" p:state="IL" p:population="2556321" />
  4. p:name="Chicago1" p:state="IL1" p:population="2556312" />
  5. p:name="Chicago2" p:state="IL2" p:population="2556311" />
  6. </util:list>

访问集合成员

  1. <property name="choseCity" value="#{cities[2]}" />
  2. <property name="choseCity" value="#{cities['Chicago']}" />

两种特殊的选择属性的方式:systemEnvironment和systemProperties。

   systemEnvironment包含了应用程序所在机器上的所有环境变量。
  1. <property name="homePath" value="#{systemEnvironment['HOME']}" />
   systemProperties包含了Java应用程序启动时所设置的所有属性。

  1. <property name="homePath" value="#{systemProperties['app.home']}" />

查询集合成员
  从城市集合中查询人口多于10000的城市。
  使用查询运算符(.?[])

  1. <property name="bigCities" value="#{cities.?[population gt 10000]}" />

使用".^[]"和".$[]",从集合中查询第一匹配项和最后一个匹配项。

 投影集合
 集合投影是从集合的每一个成员中选择特定的属性放入一个新的集合中。
 用(.![])投影运算符投影集合。
  1. <property name="cityNames" value="#{cities.![name]}" />

Spring学习笔记—装配Bean的更多相关文章

  1. #Spring实战第二章学习笔记————装配Bean

    Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...

  2. spring学习总结——装配Bean学习二(JavaConfig装配bean)

    通过Java代码装配bean 前言:上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean: 使用场景:比如说,你想要将第三方库中的组件装配到你的应用中,在 ...

  3. spring学习总结——装配Bean学习三(xml装配bean)

    通过XML装配bean Spring现在有了强大的自动化配置和基于Java的配置,XML不应该再是你的第一选择了.不过,鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中使用 ...

  4. spring学习总结——装配Bean学习一(自动装配)

    一.Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当描 ...

  5. Spring学习笔记--注入Bean属性

    这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...

  6. Spring学习(二)--装配Bean

    一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...

  7. Spring学习笔记-装配Bean-02

    什么是装配 创建应用对象之间写作关系的行为通常称为装配(wiring),这也是依赖注入(DI)的本质. Spring配置的可选方案 Spring提供了3中主要的装配机制: ● 在XML中进行显式配置. ...

  8. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

  9. spring学习总结——装配Bean学习四(导入和混合配置)

    情景:在典型的Spring应用中,我们可能会同时使用自动化和显式配置(JavaConfig)或者XML配置,幸好在Spring中,这些配置方案都不是互斥的.你尽可以将JavaConfig的组件扫描和自 ...

随机推荐

  1. C语言 homework(4)

    #include <stdio.h> int main(){ ; i=; ) { sum+=i; i++; } printf("sum=%d\n",sum); ; } ...

  2. OpenCV中对图像进行二值化的关键函数——cvThreshold()。

    函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvThreshold( const CvArr* src, CvArr* dst, double threshold, doub ...

  3. echarts之字符云tooltip显示混乱问题的解决办法

    echarts字符云中tooltip显示混乱主要表现为一下两点: 1.字体与其显示框内容不对应鼠标识别错误 解决思路: 就是option里的数据要对value降序排序(这一点很关键,是必须的一步) 把 ...

  4. ndk学习14: 进程

    Linux进程管理 来自为知笔记(Wiz)

  5. Response.Redirect()、Server.Execute和Server.Transfer的区别

    1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...

  6. STL 阅读(浅析)

    写的不错,决定那这个看下.看的还是晕. http://luohongcheng.github.io/archives/

  7. MongoDB 3.0 新特性【转】

    本文来自:http://www.open-open.com/lib/view/open1427078982824.html#_label3 更多信息见官网: http://docs.mongodb.o ...

  8. MAC OS X 常用通用快捷键

    注:由于使用的是Windows键盘,习惯了Ctrl + c/v复制粘贴,所以修改了修饰键,Command(⌘)键和Control(^)键互换,以下的Ctrl键均为Command键,对应键盘上的实际左C ...

  9. BM算法和Sunday快速字符串匹配算法

    BM算法研究了很久了,说实话BM算法的资料还是比较少的,之前找了个资料看了,还是觉得有点生涩难懂,找了篇更好的和算法更好的,总算是把BM算法搞懂了. 1977年,Robert S.Boyer和J St ...

  10. Maven 安装

    简单记录maven的安装步骤: 在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量. 下载maven3,最新版本是Maven3.2.3 ,下载地址:http://maven ...