Spring EL Hello世界示例

Spring EL与OGNL和JSF EL相似,并在bean创建期间进行评估或执行。 另外,所有Spring表达式都可以通过XML或注释使用。

在本教程中,我们向您展示如何使用Spring Expression Language(SpEL)将String,integer和bean注入XML和注解属性中。

1. Spring EL依赖

在Maven pom.xml文件中声明核心Spring jar,它将自动下载Spring EL依赖项。

档案:pom.xml

<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>
	
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
	<dependencies>

2.四季豆

两个简单的bean,后来使用SpEL将值以XML和注释形式注入属性。

package com.mkyong.core;

public class Customer {

	private Item item;

	private String itemName;

}
package com.mkyong.core;

public class Item {

	private String name;

	private int qty;

}

3. XML中的Spring EL

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.mkyong.core.Item">
		<property name="name" value="itemA" />
		<property name="qty" value="10" />
	</bean>

	<bean id="customerBean" class="com.mkyong.core.Customer">
		<property name="item" value="#{itemBean}" />
		<property name="itemName" value="#{itemBean.name}" />
	</bean>
	
</beans>
  1. #{itemBean} –将“ itemBean”注入“ customerBean” bean的“ item”属性。
  2. #{itemBean.name} –将“ itemBean”的“ name”属性注入“ customerBean” bean的“ itemName”属性中。

4.带有注释的Spring EL

在注释模式下查看等效版本。

注意
要在注释中使用SpEL,必须通过注释注册组件。 如果使用XML注册bean并在Java类中定义@Value ,则@Value将无法执行。

package com.mkyong.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.mkyong.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.mkyong.core" />

</beans>

在注释模式下,可以使用@Value定义Spring EL。 在这种情况下,您可以将String和Integer值直接注入到“ itemBean ”中,然后再将“ itemBean”注入到“ customerBean ”属性中。

5.输出

运行它,XML中的SpEL和注释都显示相同的结果:

package com.mkyong.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("SpringBeans.xml");

	    Customer obj = (Customer) context.getBean("customerBean");
	    System.out.println(obj);
	}
}

输出量

Customer [item=Item [name=itemA, qty=10], itemName=itemA]

下载源代码

下载它– Spring3-EL-Hello-Worldr-Example.zip (6 KB)

参考

  1. Spring EL参考

翻译自: https://mkyong.com/spring3/spring-el-hello-world-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值