Spring笔记2——Spring中Bean的装配
1、引言
2、声明Bean

简单声明Bean的实例:
package com.springinaction.springidol;
public class Juggler implements Performer {
private int beanBags = 3; public Juggler(){
} public Juggler( int beanBags){
this. beanBags = beanBags;
} public void perform() throws PerformanceException {
System. out.println( "抛掷 "+ beanBags + "个豆袋子" );
}
}
<?xml version="1.0" encoding= "UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id ="duke" class= "com.springinaction.springidol.Juggler" >
<constructor-arg value ="15" />
</bean >
</beans>
3、Spring的常用装配Bean值的方法
<?xml version="1.0" encoding= "UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--构造器注入值 -->
<bean id ="duke" class= "com.springinaction.springidol.Juggler" >
<constructor-arg value ="15" />
</bean >
<bean id ="sonnet29" class= "com.springinaction.springidol.Sonnet29" >
</bean >
<bean id ="poeticDuke" class= "com.springinaction.springidol.PoeticJuggler" >
<constructor-arg value ="15" />
<!--构造器注入对象引用 -->
<constructor-arg ref ="sonnet29" />
</bean >
<!--通过工厂方法创建Bean,适合单例设计模式 -->
<bean id ="theStage" class= "com.springinaction.springidol.Stage"
factory-method= "getInstance">
</bean > <!--初始化和销毁Bean -->
<bean id ="auditorium" class= "com.springinaction.springidol.Auditorium"
init-method= "turnOnLights" destroy-method="turnOffLights" >
</bean >
<!--注入属性值 -->
<bean id ="kenny" class= "com.springinaction.springidol.Instrumentalist" >
<!--注入简单值 -->
<property name ="song" value="Jingle Bells" />
<!--注入引用 -->
<property name ="instrument" ref="pinao" />
<!-- 内部Bean(inner bean) -->
<!-- <property name="instrument"> <bean class="com.springinaction.springidol.Saxophone"></bean>
</property> -->
</bean >
<!--装配集合 -->
<bean id ="hank" class= "com.springinaction.springidol.OneManBand" >
<property name ="instruments">
<list >
<ref bean ="pinao" />
<ref bean ="saxophon" />
</list >
</property >
</bean >
<!--装配Map集合 -->
<bean id ="hank2" class= "com.springinaction.springidol.OneManBand2" >
<property name ="instruments">
<map >
<entry key ="PINAO" value-ref="pinao" />
<entry key ="SAXOPHON" value-ref="saxophon" />
</map >
</property >
</bean >
<!--装配Properties集合 -->
<bean id ="hank3" class= "com.springinaction.springidol.OneManBand3" >
<property name ="instruments">
<props >
<prop key ="PINAO">TOOT TOOT TOOT</ prop>
<prop key ="SAXOPHON">PLINK PLINK PLINK</prop>
</props >
</property >
</bean >
<!--装配空值 -->
<bean id="example" class="com.springinaction.springidol.OneManBand2">
<property name="someNonNullProperty">
<null />
</property>
</bean>
<bean id ="saxophon" class= "com.springinaction.springidol.Saxophone" ></bean >
<bean id ="pinao" class= "com.springinaction.springidol.Pinao" ></bean >
</beans>
4、SPEL(Spring表达式语言)
<?xml version="1.0" encoding= "UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 字面值 -->
<bean id ="duke" class= "com.springinaction.springidol.Juggler" >
<!-- 整形 -->
<property name ="song" value="#{5}" />
<!-- 混合表达 -->
<property name ="song" value="The Value is #{5}" />
<!--浮点数 -->
<property name ="song" value="#{89.7}" />
<!--科学计数法 -->
<property name ="song" value="#{1e4}" />
<!--单引号或双引号为字符串 -->
<property name ="song" value="#{'string'}" />
<property name ="song" value='#{"string"}' />
<!--布尔值 -->
<property name ="song" value="#{false}" />
</bean > <!-- 引用Bean、Properties和方法 -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist" >
<!-- 使用SPEL表达式装配Bean值 -->
<property name ="instrument" value="#{saxophone}" />
<!-- 使用SPEL表达式装配属性值 -->
<property name ="song" value="#{kenny.song}" />
<!-- 使用SPEL表达式调用其他Bean的方法 -->
<property name ="song" value= "#{songSelector.selectSong()?.toUpperCase()}" />
</bean > <!-- 操作类,T()会调用类作用域的方法和常量 -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist" >
<!-- 使用SPEL表达式获取PI值 -->
<property name ="multiolier" value= "#{T(java.lang.Math).PI}" />
<!-- 使用SPEL表达式获取随机数 -->
<property name ="song" value= "#{T(java.lang.Math).random()}" />
</bean > <!-- SPEL进行数值计算 -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist" >
<!-- 使用SPEL进行常规计算 -->
<property name ="adjustedAmount" value= "#{counter.total+42}" />
<!-- 使用SPEL进行乘法运算 -->
<property name ="adjustedAmount" value="#{2 * T(java.lang.Math).PI * circle.radius}" />
<!-- 使用SPEL进行除法运算 -->
<property name ="adjustedAmount" value= "#{counter.total / counter.count}" />
<!-- 使用SPEL进行求余运算 -->
<property name ="adjustedAmount" value= "#{counter.total % counter.count}}" />
<!-- 不同于Java,SPEL还提供乘方运算 -->
<property name ="adjustedAmount"
value= "#{2 * T(java.lang.Math).PI * circle.radius ^ 2}" />
<!-- 使用SPEL还可以使用+号进行字符串的连接 -->
<property name ="adjustedAmount"
value= "#{performer.firstName + '' + performer.lastName}" />
</bean > <!-- SPEL进行比较 -->
<bean id ="car2" class= "com.springinaction.springidol.Instrumentalist" >
<!-- 判断条件是否成立,进行装配布尔值属性,并且Spring中使用le和ge替代<=和>=号 -->
<property name ="equal" value="#{counter.total == 100}" />
</bean > <!-- SPEL的逻辑表达式 and、or、not或! -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist2" >
<property name ="largeCircle"
value= "#{shape.kind == 'circle' and shape.perimeter gt 1000}" />
<property name ="outOfStock" value= "#{!product.avaliable}" />
</bean > <!-- SPEL的条件表达式 -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist2" >
<property name ="song"
value= "#{kenny.song != null ? kenny.song : 'Greensleeves'}" />
<!-- 三元运算符的简化形式 ,这被称为elvis运算符 -->
<property name ="song" value="#{kenny.song ?: 'Greensleeves'}" />
</bean > <!-- SPEL的正则表达式 -->
<bean id ="car1" class= "com.springinaction.springidol.Instrumentalist2" >
<property name ="validEmail"
value= "#{admin.email matches '[a-zA-Z0-9._%+-]+\\.com'}" />
</bean >
</beans>
SPEL的重要功能,筛选集合实例:
<?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:p= "http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd"> <!-- 使用Spring设置列表 -->
<util:list id ="cities">
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage1"
p:state= "1L" p:population ="111111111"></ bean>
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage2"
p:state= "2L" p:population ="211111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage3"
p:state= "3L" p:population ="311111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage4"
p:state= "4L" p:population ="411111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage5"
p:state= "5L" p:population ="511111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage6"
p:state= "6L" p:population ="611111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage7"
p:state= "7L" p:population ="711111111">
</bean >
<bean class ="com.springinaction.springidol.spel.City" p:name= "Chicage8"
p:state= "8L" p:population ="811111111">
</bean >
</util:list > <!-- SPEL筛选集合 -->
<bean id ="spelSelectSet" class= "com.springinaction.springidol.spel.ChooseCity" >
<!-- 选择第三个城市 -->
<!-- <property name="chooseCitySet" value="#{cities[2]}"></property> -->
<!-- 随机选择城市 -->
<property name ="chooseCitySet"
value= "#{cities[T(java.lang.Math).random() * cities.size()]}"></property>
<!-- 系统环境 -->
<property name ="systemEnviorment" value= "#{systemEnvironment['JAVA_HOME']}" ></property >
<!-- 系统配置信息 -->
<property name ="systemProperties" value= "#{systemProperties['path']}"></property >
<!-- 查询集合成员.?[]运算符 -->
<property name ="bigCities" value= "#{cities.?[population gt 400000000]}" ></property >
<!-- 查询第一个符合条件.^[]运算符 -->
<property name ="firstBigCity" value= "#{cities.^[population gt 400000000]}" ></property >
<!-- 查询最后一个符合条件.$[]运算符 -->
<property name ="lastBigCity" value= "#{cities.$[population gt 400000000]}" ></property >
<!-- 集合投影,即将将每一个成员中选取特有的属性放入新集合中 ,运算符.![] -->
<property name ="cityNames" value= "#{cities.?[population gt 400000000].![name + ',' +state]}"></ property>
</bean >
</beans>
Spring笔记2——Spring中Bean的装配的更多相关文章
- Spring 中Bean的装配方式
最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...
- java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
Spring 1.1.1.1 创建一个bean package com.zt.spring; public class MyBean { private String userName; privat ...
- Spring XML配置里的Bean自动装配
Spring自动装配 这段是我们之前编写的代码,代码中我们使用了P命名空间 并且使用手动装配的方式将car <bean id="address" class="cn ...
- Spring框架第二篇之Bean的装配
一.默认装配方式 代码通过getBean();方式从容器中获取指定的Bean实例,容器首先会调用Bean类的无参构造器,创建空值的实例对象. 举例: 首先我在applicationContext.xm ...
- 《Spring实战》系列之Bean的装配-Days02
2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- Spring笔记1——Spring起源及其核心技术
Spring的作用 当我们使用一种技术时,需要思考为什么要使用这门技术.而我们为什么要使用Spring呢?从表面上面SSH这三大框架中,Struts是负责MVC责任的分离,并且提供为Web层提供诸如控 ...
随机推荐
- 170406、用uid分库,uname(用户名)上的查询怎么办
[缘起] 用户中心是几乎每一个公司必备的基础服务,用户注册.登录.信息查询与修改都离不开用户中心. 当数据量越来越大时,需要多用户中心进行水平切分.最常见的水平切分方式,按照uid取模分库: 通过ui ...
- 160615、Spring3 MVC 拦截器拦截不到的问题
昨天项目组有个成员使用拦截器的时候发现不起作用,后来发现了原因,在这里跟大家分享一下(主要是冲突了).分享的是一位网友写的文章,他总结的很好. com.zk.interceptors.MyInterc ...
- http协议------>概述和动手实践认识Http协议
http协议是用来定义客户端和web服务器通讯格式 浏览器与服务器的交互过程 是tcp/ip的应用层 版本:http/1.0(客户端和web服务器建立连接后只能访问一个web资源) http/1. ...
- [转]通过apk签名使应用程序有系统权限
[转]通过apk签名使应用程序有系统权限 (2013-01-08 13:40:50) 转载▼ 标签: it 分类: Android 出处:http://blog.csdn.net/doom66151/ ...
- Zipline Risk and Performance Metrics
Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...
- JS代码识别扫码设备
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 又一次认识java(四) — 组合、聚合与继承的爱恨情仇
有人学了继承,认为他是面向对象特点之中的一个,就在全部能用到继承的地方使用继承,而不考虑到底该不该使用,无疑.这是错误的.那么.到底该怎样使用继承呢? java中类与类之间的关系 大部分的刚開始学习的 ...
- 缓存系统MemCached的Java客户端优化历程
Memcached 是什么? Memcached是一种集中式Cache,支持分布式横向扩展.这里需要解释说明一下,很多开发者觉得Memcached是一种分布式缓存系统,但是其实Memcached服务端 ...
- IDOC 实例测试
这份文档主要是自己学习IDOC的一些练习过程及心得,可能讲的不全面,但应该可以帮助大家了解IDOC的一些工作方式. IDOC或者说是ALE,事实上,是SAP用于分布和集成数据的一种方式.所以,我个人就 ...
- 160726 smarty 笔记(1)
模板里面显示变量:1.变量要写在Smarty标记之间,变量名以$开头<{$test}>2.支持所有类的数据,包括数组(关联数组),对象关联数组在模板显示的时候,除了可以使用php语法之外, ...