1.SpringEL初始

SpringEL初始

文章目录

什么是SpringEL

  • Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解
  • 我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释

Spring Beans

两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。

java 复制代码
public class Customer {

	private Item item;

	private String itemName;

}
java 复制代码
public class Item {

	private String name;

	private int qty;

}

Spring EL以XML形式

  • 使用 SpEL关闭的#{ SpEL expression }括号,请参阅XML bean定义文件下面的例子
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>
  1. #{itemBean} -- 注入"itemBean"到"customerBean"Bean 的"item"属性。
  2. #{itemBean.name} -- 注入"itemBean"的"name"属性到 "customerBean" bean的"itemname"属性。

Spring EL以注解形式

在注解使用使用SpEL,必须通过注解注册您的组件。如果注册bean在XML和Java类中定义@Value,该@Value将无法执行

java 复制代码
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;

	//...

}
java 复制代码
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;
	}

	//...
}

启用自动组件扫描

xml 复制代码
<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>

在注解模式下,可以使用@Value定义Spring EL。在这种情况下,一个String和Integer值直接注入到"itemBean",之后又注入"itemBean"到"customerBean"属性

执行输出

运行它,无论是使用 SpEL在XML 还是注释都显示了同样的结果:

java 复制代码
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]
相关推荐
小码农<^_^>8 分钟前
c++继承(下)
开发语言·c++
非著名架构师11 分钟前
js混淆的方式方法
开发语言·javascript·ecmascript
Themberfue12 分钟前
基础算法之双指针--Java实现(下)--LeetCode题解:有效三角形的个数-查找总价格为目标值的两个商品-三数之和-四数之和
java·开发语言·学习·算法·leetcode·双指针
深山夕照深秋雨mo21 分钟前
在Java中操作Redis
java·开发语言·redis
努力的布布26 分钟前
SpringMVC源码-AbstractHandlerMethodMapping处理器映射器将@Controller修饰类方法存储到处理器映射器
java·后端·spring
xujinwei_gingko27 分钟前
Spring MVC 常用注解
java·spring·mvc
PacosonSWJTU31 分钟前
spring揭秘25-springmvc03-其他组件(文件上传+拦截器+处理器适配器+异常统一处理)
java·后端·springmvc
PacosonSWJTU33 分钟前
spring揭秘26-springmvc06-springmvc注解驱动的web应用
java·spring·springmvc
barbyQAQ39 分钟前
Qt源码阅读——事件循环
开发语言·数据库·qt
记得开心一点嘛40 分钟前
在Java项目中如何使用Scala实现尾递归优化来解决爆栈问题
开发语言·后端·scala