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层提供诸如控 ...
随机推荐
- Python全栈day10(基础知识练习题)
一,执行python的两种方式 1,写在脚本里面调用python执行,例如python test.py 2, 输入python在命令行交互执行,例如 python >>> pri ...
- vim简单的配置
"设置当文件被改动时自动载入 set autoread "在左侧行号 set nu "tab长度为4 "覆盖文件时不备份 set nobackup " ...
- JAR包中的MANIFEST.MF文件详解以及编写规范
参考百度百科的解释如下: http://baike.baidu.com/item/MANIFEST.MF MANIFEST.MF:这个 manifest 文件定义了与扩展和包相关的数据.单词“mani ...
- lsof,fuser,xargs,print0,cut,paste,cat,tac,rev,exec,{},双引号,单引号,‘(字符串中执行命令)
cut用来从文本文件或标准输出中抽取数据列或者域,然后再用paste可以将这些数据粘贴起来形成相关文件. 粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同.paste将按行将不同文件行 ...
- puppeteer部署到centOS上出现launch chrome fail的情况
在Mac上调试无问题,放到阿里云上运行会报错. 需要先安装依赖, yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 lib ...
- golang-imagick图片去除边框(黑边,白边,),添加中文字
图片去除边框(黑边,白边) package main import ( "fmt" "github.com/gographics/imagick/imagick" ...
- 一条SQL引起的雪崩
1.问题描述 MySQL服务器卡死,CPU飚到300%多,命令执行缓慢. 2.问题定位 踩了狗屎运,直接找到了问题缘由 发现了一条SQL写的模糊匹配,将%写在了关键字的前面,这样会造成查询不使用索引, ...
- Python高级教程-多重继承
多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类可以扩展父类的功能. Animal类的层次设计,假设要实现以下4中动物: Dog - 狗狗: Bat - 蝙蝠: Parrot - 鹦鹉 ...
- MyEclipse安装主题(Color Theme)
前段时间发现同学开发使用IDE界面相当炫酷(Mac版的IntelliJ IDEA),个人也比较喜欢那种风格的界面,想想自己MyEclipse IDE界面简直是屌丝啊~~~~ 今天上CSDN看见一篇介绍 ...
- idea中导入本地jar包
idea中有时需要从本地导入jar包. 1:file>projectstructure 或者右键项目 open module setting, 选择librarys,然后点击+号,选择要导入的语 ...