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层提供诸如控 ...
随机推荐
- Vue基础-作用域插槽-列表组件
Vue 测试版本:Vue.js v2.5.13 Vue 官网介绍作用域插槽时, 在 2.5.0+,slot-scope 能被用在任意元素或组件中而不再局限于 <template>. 作用域 ...
- CH5301 石子合并【区间dp】
5301 石子合并 0x50「动态规划」例题 描述 设有N堆沙子排成一排,其编号为1,2,3,…,N(N<=300).每堆沙子有一定的数量,可以用一个整数来描述,现在要将这N堆沙子合并成为一堆, ...
- 谨防in、or 公用性能问题
今天遇到一个奇葩的问题:在where条件中用了 m in(×××) or m>=10,查询直接超时,我看了一下,数据库中就2万条数据 我将查询改为了union all 结果就不超时了
- jsp+servlet+mvc模式图
在我们的开发中,最常用的开发模式莫过于MVC模式,即M--MODEL.V--View.C--Controller,这样不仅可以方便开发人员分工协作,提高开发效率,增强程序的可维护性和拓展性,而且利用C ...
- git学习(4)远程库和分支管理
git学习(4)远程库和分支管理 1.1建立本地git库和远程库联系 我使用的是GitHub上的库,首先在GitHub上新建一个库,在建立与远程库的联系之前需要建立ssh key.建立ssh key可 ...
- Notice: Undefined index: wjs_cookie
w执行顺序. ok <!doctype html> <html> <head> <meta charset="UTF-8"> < ...
- Logstash之时区问题的建议和修改---filter---and duplicate resolution.
2. logstash es duplicate https://logstash.jira.com/browse/LOGSTASH-1875 https://logstash.jira.com/br ...
- echarts学习心得1---模块化单文件引入和标签式单文件引入
一.模块化单文件引入 1. 为ECharts准备一个具备大小(宽高)的Dom(当然可以是动态生成的) <div id="main" style="height:40 ...
- How many---hdu2609(最小表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 给你n个01串,然后求这n个串中有几个不同的: 例如:1100 ,1001 ,0011 ,011 ...
- Spring Bean声明周期
Bean的生命周期 理解Spring Bean的生命周期很容易.当一个bean被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当bean不再需要,并且从容器中移除时,可能需要做一些清除工 ...