</bean>
<!--bean配置继承:使用bean的parent。
spring允许继承bean的配置,被继承的bean称为父bean.继承这个父bean的bean称为bean。
子bean从父bean中继承配置,包括bean的属性配置。子bean也可以腐败从父bean继承过来的属性。
父bean作为模板,可以设置<bean>作为abstract属性为true。这样的spring将不会实例化这个bean。
并不是<bean>元素里的所有属性,都会被继承,autowire abstract等。
也可以忽略父bean的class属性,让子bean指定自己的类,而共享相同的属性配置,但此时abstract必须设置为true。
如果某个bean的class属性为空 那么这个bean只能为抽象的 -->
<bean id="addres2" parent="address" p:city="hanna " p:street="DaZhongSi"></bean>
<bean id="car" class="com.spring.beans.autowires.Car" p:brand="AUDI" p:price="30000"></bean>
<!-- 要求在配置Person时必须有一个关联的car。换句话说,person这个bean依赖于Car这个bean
Spring 允许用户通过depends-on属性设定Bean前置依赖的Bean。前置依赖的bean会在本Bean实例化之前创建好,
如果前置依赖多个Bean,则可以通过逗号,空格的方式配置Bean名称。-->
<bean id="person" class="com.spring.beans.autowires.Person" depends-on="car" p:name="Tom1" p:address-ref="addres2"></bean>
</beans>
下边是关于SpeEL的解释及初步使用。
spirng 表达式语言:SpEL
Spring 表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言
语法类似于EL:SpEL使用#{}作为定界符,所有在大框号中的字符将被认为是SpEL
SpEL为bean属性进行动态赋值提供了便利,
通过SpEL可以实现
通过bean的id对bean进行引用,
调用方法以及引用对象中属性。
计算表达式值。
正则表达式的匹配。
SpEL:字面量的表示:
整数:<property name="count" value="#{5}"/>
小数:<property name="frequency" value="#{89.4}"/>
科学计数法:<property name="capacity" value="1e4"/>
String 可以使用单引号或者双引号作为字符串定界符,<property name="name" value="#{'Chuck'}"/>或<property name="name" value='#{"Chuck"}'/>
Boolean <property name="enable" value="#{false}">
引用Bea。属性和方法
引用其他对象
<!--通过value属性和SpEl配置之间的应用联系-->
<property name="prefix" value="#{prefixGenerator}"></property>
引用其他对象的属性
<!--通过value属性和SpEL配置suffix属性值为另一个Bean的suffix属性值-->
<property name="suffix" value="#{sequenceGenerator2.suffix}">
<!--调用其他方法,还可以链试操作-->
<!--通过value属性和spel配置suffix属性值为另一个Bean的方法的返回值-->
<property name="suffix" value="#{sequenceGenerator.toString()}"/>
<!--方法的连缀-->
<property name="suffix" value="#{sequenceGenerator2.toString().toUpperCase()}">
SpEL:支持绝大多数的算术运算符。
SpEL:应用Bean,属性和方法。
调用静态方法或静态属性,通过T()调用一个类的静态方法,它将返回一个Class Object然后在调用相应的方法或者属性。
<property name="initValue" value="#{T(java.lang.Math).PI }">
<?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="address" class="com.spring.beans.autowires.spel.Address">
<!-- 使用spel为属性赋值(字面值) -->
<property name="city" value="#{'BEIJING'}"></property>
<property name="street" value="#{'WuDaoKou'}"></property>
</bean>
<bean id="car" class="com.spring.beans.autowires.spel.Car">
<property name="brand" value="Audi-1"></property>
<property name="price" value="290000"></property>
<!-- 使用SpeEL引用类的静态属性-->
<property name="tyreperimeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean>
<bean id="person" class="com.spring.beans.autowires.spel.Person">
<!-- 使用Spel来应用其他的Bean -->
<property name="car" value="#{car}"></property>
<!-- 使用address的city属性 -->
<property name="city" value="#{address.city}"></property>
<property name="info" value="#{car.price>=300000?'金领':'白领'}"></property>
<property name="name" value="WEI"></property>
</bean>
</beans>