1.bean
package spring.bean;
public class example {
private String s1;
private String s2;
private String s3;
private String s4;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public String getS2() {
return s2;
}
public void setS2(String s2) {
this.s2 = s2;
}
public String getS3() {
return s3;
}
public void setS3(String s3) {
this.s3 = s3;
}
public String getS4() {
return s4;
}
public void setS4(String s4) {
this.s4 = s4;
}
@Override
public String toString() {
return "example{" +
"s1='" + s1 + '\'' +
", s2='" + s2 + '\'' +
", s3='" + s3 + '\'' +
", s4='" + s4 + '\'' +
'}';
}
public static String getString(){
return "现在的字符串是静态方法制造的";
}
public String getString1(){
return "现在的字符串是普通方法创造的";
}
}
2.beans2.xml
<?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">
<!--SpringEL表达式-->
<bean id="example1" class="spring.bean.example"></bean>
<bean id="example" class="spring.bean.example">
<!--字符串-->
<property name="s1" value="#{'常规创造String'}"></property>
<!--其他类的方法-->
<property name="s2" value="#{example1.getString1()}"></property>
<!--其他类的静态方法-->
<property name="s3" value="#{T(spring.bean.example).string}"></property>
</bean>
</beans>
3.test
package spring.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.bean.example;
import spring.bean.student;
public class test3 {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
example example= (spring.bean.example) context.getBean("example");
System.out.println(example);
}
}
4.结果
example{s1='常规创造String', s2='现在的字符串是普通方法创造的', s3='现在的字符串是静态方法制造的', s4='null'}
5.感想
(1)其实这个表达式的扩展能力很强,比如调用普通方法之后得到的String可以进一步调用String的方法去得到其他对象。
本文探讨了Spring Expression Language(EL)在bean、beans2.xml配置及测试中的应用。通过实例解析,展示了EL如何调用普通方法并进一步处理返回的String对象,揭示了其强大的扩展能力和灵活性。
1455

被折叠的 条评论
为什么被折叠?



