Spring笔记2——Spring中Bean的装配
1、引言
Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象。创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入的本质。
2、声明Bean
配置Bean的方式主要有两种:基于XML文件的配置方式和基于Java注解的配置方式。传统的基于XML文件的配置方式在声明Bean时,Spring配置文件的根元素来源于Spring beans命名空间所定义的<beans>元素。除了beans命名空间外,Java自带了多种XML命名空间,通过这些命名空间可以配置Spring容器 。
下图为一些常见的Spring命名空间:
简单声明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表达式语言)
Spring还可以使用表达式进行装配,Spring使用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" 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>