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层提供诸如控 ...
随机推荐
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
Java代码 <!-- 指明spring配置文件在何处 --> <context-param> <param-name>contextConfigLocation& ...
- Python全栈day20(装饰器基本理论)
一,什么是装饰器 装饰器:本质就是函数,功能是为其他函数添加附加功能 原则 1,不修改被修饰函数的源代码 2,不修改被修饰函数的调用方式 举例说明:有一个求和函数要求就算出函数的运行时间 正常代码应该 ...
- Linux 常用资源
kernel:ftp://kernel.orgcnkernel:http://www.cnkernel.orgoldlinux:http://www.oldlinux.orgminix3:http:/ ...
- 如何做rom,体验做rom过程,附图文教程,感谢各位romer
http://bbs.gfan.com/android-5408130-1-1.html 有人问我,我简单的写一下,来源XDA,运行环境ubuntu 10.4. ubuntu安装很简单,在window ...
- 前端开发 - JavaScript - 上
1.js引入 <!DOCTYPE html> <html lang="cn"> <head> <meta charset="UT ...
- CentOS7系统基本操作
查看网卡命令 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state ...
- LeetCode之Symmetric Tree
</pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ce ...
- 【非root用户】安装【python,pip,package】
安装python: 下载源码 解压 进入 ./configure --prefix=/path/python3.6 注意一定要设置prefix,否则默认安装到/usr/local make make ...
- linux知识体系
0. Linux简介与厂商版本 1. Linux开机启动 2. Linux文件管理 3. Linux的架构 4. Linux命令行与命令 5. Linux文件管理相关命令 6. Linux文本流 7. ...
- 17.Delete Methods-官方文档摘录
介绍delete的方法 MongoDB provides the following methods to delete documents of a collection: db.collectio ...