JSP页面使用过EL表达式:${}
Spring有Spring表达式:#{}
Spring表达式用于获取bean的属性值
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<util:list id="interestBean">
<value>钓鱼</value>
<value>国画</value>
<value>做饭</value>
</util:list>
<util:set id="cityBean">
<value>金陵</value>
<value>洛阳</value>
<value>北平</value>
</util:set>
<util:map id="scoreBean">
<entry key="english" value="90"/>
<entry key="math" value="80"/>
</util:map>
<util:properties id="dbBean">
<prop key="username">sally</prop>
<prop key="password">1234</prop>
</util:properties>
<bean id="user" class="org.spring.teach.User">
<property name="name" value="#{interestBean[0]}"/>
<property name="city" value="#{cityBean[0]}"/>
<!--
可以使用[]来访问属性,这种方式支持中文
<property name="score" value="#{scoreBean['english']}"/>-->
<property name="score" value="#{scoreBean.english}"/>
<property name="username" value="#{dbBean.username}"/>
</bean>
</beans>
public class User {
private String name;
private String city;
private String score;
private String username;
public void setName(String name) {
this.name = name;
}
public void setCity(String city) {
this.city = city;
}
public void setScore(String score) {
this.score = score;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", city='" + city + '\'' +
", score='" + score + '\'' +
", username='" + username + '\'' +
'}';
}
}
public class TestCase {
private AbstractApplicationContext app;
@Before
public void before() {
app = new ClassPathXmlApplicationContext("classpath:spring.xml");
}
@After
public void after() {
app.close();
}
@Test
public void test() {
User user = app.getBean("user", User.class);
System.out.println(user);
}
}
一月 25, 2019 12:02:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 12:02:16 CST 2019]; root of context hierarchy
一月 25, 2019 12:02:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
一月 25, 2019 12:02:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 12:02:16 CST 2019]; root of context hierarchy
User{name='钓鱼', city='金陵', score='90', username='sally'}
Process finished with exit code 0