这一章节我们来说一下SpEl表达式的坑,以及补充一下SpEl表达式操作类的方法与常量。
1.domain
厨师类:(新增一个朋友属性)
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16; public class Chief { private String name = ""; private Oven oven; private Chief friend; public Chief getFriend() { return friend; } public void setFriend(Chief friend) { this.friend = friend; } public String getName() { return name; } public Oven getOven() { return oven; } public void setName(String name) { this.name = name; } public void setOven(Oven oven) { this.oven = oven; } public void userOven() { System.out.println(name + " use " + oven); } }
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16; public class Oven { private String name = ""; private double size = 0; public double getSize() { return size; } public void setSize(double size) { this.size = size; } @Override public String toString() { return name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_16/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief jack = (Chief) applicationContext.getBean("jack"); jack.userOven(); Chief mike = (Chief) applicationContext.getBean("mike"); mike.userOven(); Chief rose = (Chief) applicationContext.getBean("rose"); rose.userOven(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="smallOven" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Oven" p:name="smallOven" p:size="#{T(java.lang.Math).PI}" /> <bean id="mike" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief" p:name="mike"> <property name="oven" ref="#{jack.oven}" /> <property name="friend" ref="jack" /> </bean> <bean id="jack" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief" p:name="jack"> <property name="oven" ref="smallOven" /> <property name="friend" ref="mike" /> </bean> <bean id="rose" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_16.Chief" p:name="rose"> <property name="oven" ref="#{jack.getFriend().getOven()}" /> <property name="friend" ref="jack" /> </bean> </beans>
(1)我们使用#{T(class).xxx}来控制类的方法与常量
(2)在执行的过程中一定要注意Bean前后属性,当上面的mike与jack互换位置的时候,就会抛异常,上面的是正确的
(3)在执行的过程中必须注意toString的问题,就像上面的#{jack.oven}和#{jack.getFriend().getOven()}其实就是先调用oven的toString方法,得到相应的名称,然后再放到配置文件去(我返回的名字跟id一致),然后再读取这个返回值的Bean在不在,如果不重写toString或者toString返回的字符串跟你Bean里面的id不匹配,直接抛异常
总结:这一章节通过一个例子来说明SpEl表达式的坑。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring