Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象。相反,容器负责把需要相互协作的对象引用赋予各个对象。创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质。
1.声明Bean
1.1 创建Spring配置
在XML文件中声明Bean时,Spring配置文件的根元素是来源于Spring beans命名空间所定义的<beans>元素。以下是一个典型的Spring XML配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <!-- Bean declaration go here --> </beans>
在<beans>元素内,可以放置所有的Spring配置信息,包括<bean>元素的声明。
Spring核心框架自带的10个命名空间配置如下:
1.2 声明一个简单的Bean
1.3 通过构造器注入
<bean id="zookeeperSources" class="com.dangdang.config.service.easyzk.support.spring.ZookeeperSourceFactoryProxy" >
<constructor-arg name="appName" value="${server.name}" />
<constructor-arg name="configFactory" ref="configFactory" />
<constructor-arg name="nodes" value=""/>
</bean>
2) 通过工厂化方法创建Bean
<bean id="zookeeperSources" class="com.dangdang.config.service.ZookeeperSource" factory-method="create"/>
1.4 Bean的作用域
<bean id="zookeeperSources" class="com.dangdang.config.service.ZookeeperSource" scope="prototype"/>
Spring的作用域选项:
1.5 初始化和销毁Bean
<bean id="auditor" class="com.springinaction.springidol.Auditor" init-method="turnOnLights" destroy-method="turnOffLights" />
可以使用<beans>元素的default-init-method和default-destroy-method为应用上下文中所有的Bean设置共同的初始化和销毁方法。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-init-method="
turnOnLights" default-destroy-method="turnOffLights">
</beans>
2.注入Bean属性
2.1注入简单值
<bean id =“kenny" class="com.ouc.springinaction.test">
<property name ="song" value="Bells" />
</bean>
2.2 引用其他Bean
<bean id =“saxon" class="com.ouc.springinaction.saxontest"></bean>
<bean id =“kenny" class="com.ouc.springinaction.test2">
<property name ="song" value="Bells" />
<property name ="saxonIns" ref="saxon" />
</bean>
Spring提倡面向接口编程,面向接口编程与依赖注入协作实现了松散耦合。
2.3 注入内部Bean
<bean id =“saxon" class="com.ouc.springinaction.saxontest"></bean>
<bean id =“kenny" class="com.ouc.springinaction.test2">
<property name ="song" value="Bells" />
<property name ="saxonIns">
<bean class="com.ouc.springinaction.saxontest">
/property>
</bean>
内部Bean是通过直接声明一个<bean>元素作为<property>元素的子节点而定义的。
2.4 使用Spring的命名空间p装配属性
<bean id =“kenny" class="com.ouc.springinaction.test3">
p:song = "Bells"
p:saxonIns-ref = "saxon" />
</bean>
使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。-ref后缀作为一个标识来告知Spring应该装配一个引用。
2.5 装配集合
<bean id =“hank" class="com.ouc.springinaction.test4">
<property name ="ins">
<set>
<ref bean = "guitar" />
<ref bean = "cymbal" />
<ref bean = "harmonica" />
<ref bean = "harmonica" />
</set>
</property>
</bean>
<bean id =“hank" class="com.ouc.springinaction.test4">
<property name ="ins">
<list>
<ref bean = "guitar" />
<ref bean = "cymbal" />
<ref bean = "harmonica" />
</list>
</property>
</bean>
装配Map集合
<bean id =“hank" class="com.ouc.springinaction.test4">
<property name ="ins">
<map>
<entry key="GUITAR" value-ref = "guitar" />
<entry key="GUITAR1" value-ref = "guitar1" />
<entry key="GUITAR2" value-ref = "guitar2" />
</map>
</property>
</bean>
<bean id =“hank" class="com.ouc.springinaction.test4">
<property name ="ins">
<props>
<prop key="GUITAR"> STRUM </prop>
<prop key="CYMBAL"> CRASH </prop>
</props>
</property>
</bean>
● <property> 元素用于把值或Bean引用注入到Bean的属性中。
● <props> 元素用于定义一个java.util.Properties类型的集合值。
<property name ="nullIns"><null/></property>
2.6 使用表达式装配
<property name="cappacity" value="#{1e4}" />
<property name="ins" value="#{saxno.song}" />
<property name="ins" value="#{saxno.song()?.toUpperCase()}" />
使用?.运算符代替点(.)来访问toUpperCase()方法。
操作类
<property name="mathplier" value="#{T(java.lang.Math).PI}" />
<property name="randomNum" value="#{T(java.lang.Math).random()}" />
使用SpEL进行数值运算
<property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}" />
比较值
<property name="hasCap" value="#{count.total le 10000}" />
逻辑表达式
<property name="outStock" value="#{!pro.available}" />
<property name="outStock" value="#{not pro.available}" />
条件表达式
<property name="song" value="#{keny.song != null ? keny.song : 'Green'}" />
<property name="song" value="#{keny.song ?: 'Green'}" />
SpEL正则表达式
<property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}" />
使用<util:list>元素在Spring里配置一个包含City对象的List集合。
<util:list id ="cities">
<bean class="com.habuma.spel.city"
p:name="Chicago" p:state="IL" p:population="2556321" />
p:name="Chicago1" p:state="IL1" p:population="2556312" />
p:name="Chicago2" p:state="IL2" p:population="2556311" />
</util:list>
访问集合成员
<property name="choseCity" value="#{cities[2]}" />
<property name="choseCity" value="#{cities['Chicago']}" />
两种特殊的选择属性的方式:systemEnvironment和systemProperties。
<property name="homePath" value="#{systemEnvironment['HOME']}" />
<property name="homePath" value="#{systemProperties['app.home']}" />
查询集合成员
从城市集合中查询人口多于10000的城市。
使用查询运算符(.?[])
<property name="bigCities" value="#{cities.?[population gt 10000]}" />
使用".^[]"和".$[]",从集合中查询第一匹配项和最后一个匹配项。
<property name="cityNames" value="#{cities.![name]}" />
Spring学习笔记—装配Bean的更多相关文章
- #Spring实战第二章学习笔记————装配Bean
Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...
- spring学习总结——装配Bean学习二(JavaConfig装配bean)
通过Java代码装配bean 前言:上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean: 使用场景:比如说,你想要将第三方库中的组件装配到你的应用中,在 ...
- spring学习总结——装配Bean学习三(xml装配bean)
通过XML装配bean Spring现在有了强大的自动化配置和基于Java的配置,XML不应该再是你的第一选择了.不过,鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中使用 ...
- spring学习总结——装配Bean学习一(自动装配)
一.Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当描 ...
- Spring学习笔记--注入Bean属性
这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...
- Spring学习(二)--装配Bean
一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...
- Spring学习笔记-装配Bean-02
什么是装配 创建应用对象之间写作关系的行为通常称为装配(wiring),这也是依赖注入(DI)的本质. Spring配置的可选方案 Spring提供了3中主要的装配机制: ● 在XML中进行显式配置. ...
- Spring学习笔记(3)——Bean的注入方式
依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...
- spring学习总结——装配Bean学习四(导入和混合配置)
情景:在典型的Spring应用中,我们可能会同时使用自动化和显式配置(JavaConfig)或者XML配置,幸好在Spring中,这些配置方案都不是互斥的.你尽可以将JavaConfig的组件扫描和自 ...
随机推荐
- huffman编码压缩算法(转)
参考:http://blog.csdn.net/sunmenggmail/article/details/7598012 笔试时遇到的一道题.
- Apache 配置参考
1.什么是Apache ? Apache,是一种开放源码的HTTP服务器,可以在大多数操作系统中运行,由于其多平台和安全性所以被广泛使用,是目前最流行的Web服务器软件之一.Apache 起初由 Il ...
- 1.4---字符串空格变成20%(CC150)
import CtCILibrary.AssortedMethods; public class Question { // Assume string has sufficient free spa ...
- 【leetcode】Substring with Concatenation of All Words
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- solr单机环境配置并包含外部单机zookeeper
首先和之前一样下载solr-5.3.1.tgz,然后执行下面命令释放文件并放置在/usr/目录下: $ .tgz $ /usr/ $ cd /usr/solr- 这个时候先不用启动solr,因为单机模 ...
- SolrCloud环境配置
Solr是一个企业级搜索服务器,对外提供Web-Service接口,用户可以通过http请求,向搜索引擎提交xml或者json格式的数据,生成索引:然后可以通过http get请求查找,获取返回的xm ...
- cxGrid 增加序号 (非数据库绑定模式) (测试通过)
cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Edi ...
- 关于MySQL count(distinct) 逻辑的一个bug【转】
本文来自:http://dinglin.iteye.com/blog/1976026#comments 背景 客户报告了一个count(distinct)语句返回结果错误,实际结果存在值,但是用cou ...
- js 一搬问题汇总
--有时无法进行js调试,在浏览器中设置启用脚本调试就可以了
- python2.79安装
从官网下载最新的安装程序,基于windows的,也可以直接百度下载 点击安装,如果其他用户不需要python的话,可以使用第二个,不过我们一般都是单用户,所以没差 选择安装路径,可按默认路径安装,也可 ...