Spring 专题
专题目录
您的位置:java > Spring专题 > Spring EL hello world实例
Spring EL hello world实例
作者:--    发布时间:2019-11-20
spring el与ognl和jsf el相似,计算评估或在bean创建时执行。此外,所有的spring表达式都可以通过xml或注解。
在本教程中,我们将学习如何使用spring表达式语言(spel),注入字符串,整数,bean到属性,无论是在xml和注释。

1. spring beans

两个简单bean,后来利用 spel 注入值到属性,在 xml 和 注释。
package com.h3.core;

public class customer {

	private item item;

	private string itemname;

}
package com.h3.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.h3.core.item">
		<property name="name" value="itema" />
		<property name="qty" value="10" />
	</bean>

	<bean id="customerbean" class="com.h3.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,必须通过注解注册您的组件。如果注册bean在xml和java类中定义@value,该@value将无法执行。
package com.h3.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.h3.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.h3.core" />

</beans>
在注解模式下,可以使用@value定义spring el。在这种情况下,一个string和integer值直接注入到“itembean”,之后又注入“itembean”到“customerbean”属性。

5. 执行输出

运行它,无论是使用 spel在xml 还是注释都显示了同样的结果:
package com.h3.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]

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册