Spring EL hello world实例
Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解。 在本教程中,我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释。 1. Spring Beans 两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。 package com.yiibai.core; public class Customer { private Item item; private String itemName; } package com.yiibai.core; public class Item { private String name; private int qty; } 3. Spring EL以XML形式 使用 SpEL关闭的#{ SpEL expression }括号,请参阅XML bean定义文件下面的例子。 <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-3.0.xsd"> <bean id="itemBean" class="com.yiibai.core.Item"> <property name="name" value="itemA" /> <property name="qty" value="10" /> </bean> <bean id="customerBean" class="com.yiibai.core.Customer"> <property name="item" value="#{itemBean}" /> <property name="itemName" value="#{itemBean.name}" /> </bean> </beans> #{itemBean}
1. Spring Beans
package com.yiibai.core;
public class Customer {
private Item item;
private String itemName;
}
package com.yiibai.core;
public class Item {
private String name;
private int qty;
}
3. Spring EL以XML形式
<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-3.0.xsd">
<bean id="itemBean" class="com.yiibai.core.Item">
<property name="name" value="itemA" />
<property name="qty" value="10" />
</bean>
<bean id="customerBean" class="com.yiibai.core.Customer">
<property name="item" value="#{itemBean}" />
<property name="itemName" value="#{itemBean.name}" />
</bean>
</beans>
- #{itemBean} – 注入“itemBean”到“customerBean”Bean 的“item”属性。
- #{itemBean.name} – 注入“itemBean”的“name”属性到 “customerBean" bean的"itemname”属性。
4. Spring EL以注解形式
package com.yiibai.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
@Value("#{itemBean}")
private Item item;
@Value("#{itemBean.name}")
private String itemName;
//...
}
package com.yiibai.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item {
@Value("itemA") //inject String directly
private String name;
@Value("10") //inject interger directly
private int qty;
public String getName() {
return name;
}
//...
}
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.yiibai.core" /> </beans>
5. 执行输出
package com.yiibai.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);
}
}
输出结果
Customer [item=Item [name=itemA, qty=10], itemName=itemA]
交流与评论