15 Spring EL hello world实例
2017-07-20 本文已影响13人
笑Skr人啊
Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解。
在本教程中,我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释。
1. Spring Beans
两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。
package com.gp6.el.hello.xml;
public class Customer {
private Item item;
private String itemName;
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
package com.gp6.el.hello.xml;
public class Item {
private String name;
private int qty;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
2.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.gp6.el.hello.xml.Item">
<property name="name" value="itemA" />
<property name="qty" value="10" />
</bean>
<bean id="customerBean" class="com.gp6.el.hello.xml.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”属性。
Spring EL以注解形式
3.请参阅等效版本注释模式。
注:要在注解使用使用SpEL,必须通过注解注册您的组件。如果注册bean在XML和Java类中定义@Value,该@Value将无法执行。
package com.gp6.el.hello.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.gp6.el.hello.xml.Item;
@Component("customerBean2")
public class Customer2 {
@Value("#{itemBean2}")
private Item2 item2;
@Value("#{itemBean2.name}")
private String itemName;
public Item2 getItem2() {
return item2;
}
public void setItem2(Item2 item2) {
this.item2 = item2;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
package com.gp6.el.hello.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean2")
public class Item2 {
@Value("itemA2")
private String name;
@Value("102")
private int qty;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
启用自动组件扫描。
<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">
<!--
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,
如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
-->
<context:component-scan base-package="com.gp6.el.hello.annotation" />
</beans>
在注解模式下,可以使用@Value定义Spring EL。在这种情况下,一个String和Integer值直接注入到“itemBean”,之后又注入“itemBean”到“customerBean”属性。
4. 执行输出
运行它,无论是使用 SpEL在XML 还是注释都显示了同样的结果:
- 通过XML方式测试文件
package com.gp6.el.hello.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main( String[] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/gp6/el/hello/xml/el_hello.xml");
Customer customer = (Customer) context.getBean("customerBean");
System.out.println(customer);
}
}
- 通过注解测试文件
package com.gp6.el.hello.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main( String[] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/gp6/el/hello/annotation/el_hello2.xml");
Customer2 customer = (Customer2) context.getBean("customerBean2");
System.out.println(customer);
}
}