- 浏览: 21437020 次
- 性别:
博客专栏
-
跟我学spring3
浏览量:2405558
-
Spring杂谈
浏览量:2998127
-
跟开涛学SpringMVC...
浏览量:5631794
-
Servlet3.1规范翻...
浏览量:257649
-
springmvc杂谈
浏览量:1593328
-
hibernate杂谈
浏览量:249020
-
跟我学Shiro
浏览量:5847952
-
跟我学Nginx+Lua开...
浏览量:698265
-
亿级流量网站架构核心技术
浏览量:780624
文章分类
- 全部博客 (329)
- 跟我学Nginx+Lua开发 (13)
- 跟我学spring (54)
- 跟开涛学SpringMVC (34)
- spring4 (16)
- spring杂谈 (50)
- springmvc杂谈 (22)
- 跟我学Shiro (26)
- shiro杂谈 (3)
- hibernate杂谈 (10)
- java开发常见问题分析 (36)
- 加速Java应用开发 (5)
- Servlet 3.1规范[翻译] (21)
- servlet3.x (2)
- websocket协议[翻译] (14)
- websocket规范[翻译] (1)
- java web (6)
- db (1)
- js & jquery & bootstrap (4)
- 非技术 (4)
- reminder[转载] (23)
- 跟叶子学把妹 (8)
- nginx (2)
- 架构 (19)
- flume架构与源码分析 (4)
最新评论
-
xxx不是你可以惹得:
认真看错误代码,有时候重启电脑就行了 醉了 我把数据库配置写死 ...
第十六章 综合实例——《跟我学Shiro》 -
dagger9527:
holyselina 写道您前面说到能获取调用是的参数数组,我 ...
【第六章】 AOP 之 6.6 通知参数 ——跟我学spring3 -
xxx不是你可以惹得:
Access denied for user 'root'@' ...
第十六章 综合实例——《跟我学Shiro》 -
dagger9527:
只有@AspectJ支持命名切入点,而Schema风格不支持命 ...
【第六章】 AOP 之 6.5 AspectJ切入点语法详解 ——跟我学spring3 -
dagger9527:
支持虽然会迟到,但永远不会缺席!
【第四章】 资源 之 4.3 访问Resource ——跟我学spring3
5.4.1 xml风格的配置
SpEL支持在Bean定义时注入,默认使用“#{SpEL表达式}”表示,其中“#root”根对象默认可以认为是ApplicationContext,只有ApplicationContext实现默认支持SpEL,获取根对象属性其实是获取容器中的Bean。
首先看下配置方式(chapter5/el1.xml)吧:
java代码:
- <bean id="world" class="java.lang.String">
- <constructor-arg value="#{' World!'}"/>
- </bean>
- <bean id="hello1" class="java.lang.String">
- <constructor-arg value="#{'Hello'}#{world}"/>
- </bean>
- <bean id="hello2" class="java.lang.String">
- <constructor-arg value="#{'Hello' + world}"/>
- <!-- 不支持嵌套的 -->
- <!--<constructor-arg value="#{'Hello'#{world}}"/>-->
- </bean>
- <bean id="hello3" class="java.lang.String">
- <constructor-arg value="#{'Hello' + @world}"/>
- </bean>
模板默认以前缀“#{”开头,以后缀“}”结尾,且不允许嵌套,如“#{'Hello'#{world}}”错误,如“#{'Hello' + world}”中“world”默认解析为Bean。当然可以使用“@bean”引用了。
接下来测试一下吧:
java代码:
- @Test
- public void testXmlExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el1.xml");
- String hello1 = ctx.getBean("hello1", String.class);
- String hello2 = ctx.getBean("hello2", String.class);
- String hello3 = ctx.getBean("hello3", String.class);
- Assert.assertEquals("Hello World!", hello1);
- Assert.assertEquals("Hello World!", hello2);
- Assert.assertEquals("Hello World!", hello3);
- }
是不是很简单,除了XML配置方式,Spring还提供一种注解方式@Value,接着往下看吧。
5.4.2 注解风格的配置
基于注解风格的SpEL配置也非常简单,使用@Value注解来指定SpEL表达式,该注解可以放到字段、方法及方法参数上。
测试Bean类如下,使用@Value来指定SpEL表达式:
java代码:
首先看下配置文件(chapter5/el2.xml):
java代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <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:annotation-config/>
- <bean id="world" class="java.lang.String">
- <constructor-arg value="#{' World!'}"/>
- </bean>
- <bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
- <bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
- <property name="value" value="haha"/>
- </bean>
- </beans>
配置时必须使用“<context:annotation-config/>”来开启对注解的支持。
有了配置文件那开始测试吧:
java代码:
- @Test
- public void testAnnotationExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el2.xml");
- SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
- Assert.assertEquals("Hello World!", helloBean1.getValue());
- SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
- Assert.assertEquals("haha", helloBean2.getValue());
- }
其中“helloBean1 ”值是SpEL表达式的值,而“helloBean2”是通过setter注入的值,这说明setter注入将覆盖@Value的值。
5.4.3 在Bean定义中SpEL的问题
如果有同学问“#{我不是SpEL表达式}”不是SpEL表达式,而是公司内部的模板,想换个前缀和后缀该如何实现呢?
那我们来看下Spring如何在IoC容器内使用BeanExpressionResolver接口实现来求值SpEL表达式,那如果我们通过某种方式获取该接口实现,然后把前缀后缀修改了不就可以了。
此处我们使用BeanFactoryPostProcessor接口提供postProcessBeanFactory回调方法,它是在IoC容器创建好但还未进行任何Bean初始化时被ApplicationContext实现调用,因此在这个阶段把SpEL前缀及后缀修改掉是安全的,具体代码如下:
java代码:
- package cn.javass.spring.chapter5;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
- import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- import org.springframework.context.expression.StandardBeanExpressionResolver;
- public class SpELBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
- throws BeansException {
- StandardBeanExpressionResolver resolver = (StandardBeanExpressionResolver) beanFactory.getBeanExpressionResolver();
- resolver.setExpressionPrefix("%{");
- resolver.setExpressionSuffix("}");
- }
- }
首先通过 ConfigurableListableBeanFactory的getBeanExpressionResolver方法获取BeanExpressionResolver实现,其次强制类型转换为StandardBeanExpressionResolver,其为Spring默认实现,然后改掉前缀及后缀。
开始测试吧,首先准备配置文件(chapter5/el3.xml):
java代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <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:annotation-config/>
- <bean class="cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor"/>
- <bean id="world" class="java.lang.String">
- <constructor-arg value="%{' World!'}"/>
- </bean>
- <bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
- <bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
- <property name="value" value="%{'Hello' + world}"/>
- </bean>
- </beans>
配置文件和注解风格的几乎一样,只有SpEL表达式前缀变为“%{”了,并且注册了“cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor”Bean,用于修改前缀和后缀的。
写测试代码测试一下吧:
java代码:
- @Test
- public void testPrefixExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el3.xml");
- SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
- Assert.assertEquals("#{'Hello' + world}", helloBean1.getValue());
- SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
- Assert.assertEquals("Hello World!", helloBean2.getValue());
- }
此处helloBean1 中通过@Value注入的“#{'Hello' + world}”结果还是“#{'Hello' + world}”说明不对其进行SpEL表达式求值了,而helloBean2使用“%{'Hello' + world}”注入,得到正确的“"Hello World!”。
- <bean id="world" class="java.lang.String">
- <constructor-arg value="#{' World!'}"/>
- </bean>
- <bean id="hello1" class="java.lang.String">
- <constructor-arg value="#{'Hello'}#{world}"/>
- </bean>
- <bean id="hello2" class="java.lang.String">
- <constructor-arg value="#{'Hello' + world}"/>
- <!-- 不支持嵌套的 -->
- <!--<constructor-arg value="#{'Hello'#{world}}"/>-->
- </bean>
- <bean id="hello3" class="java.lang.String">
- <constructor-arg value="#{'Hello' + @world}"/>
- </bean>
- @Test
- public void testXmlExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el1.xml");
- String hello1 = ctx.getBean("hello1", String.class);
- String hello2 = ctx.getBean("hello2", String.class);
- String hello3 = ctx.getBean("hello3", String.class);
- Assert.assertEquals("Hello World!", hello1);
- Assert.assertEquals("Hello World!", hello2);
- Assert.assertEquals("Hello World!", hello3);
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <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:annotation-config/>
- <bean id="world" class="java.lang.String">
- <constructor-arg value="#{' World!'}"/>
- </bean>
- <bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
- <bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
- <property name="value" value="haha"/>
- </bean>
- </beans>
- @Test
- public void testAnnotationExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el2.xml");
- SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
- Assert.assertEquals("Hello World!", helloBean1.getValue());
- SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
- Assert.assertEquals("haha", helloBean2.getValue());
- }
- package cn.javass.spring.chapter5;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
- import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
- import org.springframework.context.expression.StandardBeanExpressionResolver;
- public class SpELBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
- throws BeansException {
- StandardBeanExpressionResolver resolver = (StandardBeanExpressionResolver) beanFactory.getBeanExpressionResolver();
- resolver.setExpressionPrefix("%{");
- resolver.setExpressionSuffix("}");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <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:annotation-config/>
- <bean class="cn.javass.spring.chapter5.SpELBeanFactoryPostProcessor"/>
- <bean id="world" class="java.lang.String">
- <constructor-arg value="%{' World!'}"/>
- </bean>
- <bean id="helloBean1" class="cn.javass.spring.chapter5.SpELBean"/>
- <bean id="helloBean2" class="cn.javass.spring.chapter5.SpELBean">
- <property name="value" value="%{'Hello' + world}"/>
- </bean>
- </beans>
- @Test
- public void testPrefixExpression() {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter5/el3.xml");
- SpELBean helloBean1 = ctx.getBean("helloBean1", SpELBean.class);
- Assert.assertEquals("#{'Hello' + world}", helloBean1.getValue());
- SpELBean helloBean2 = ctx.getBean("helloBean2", SpELBean.class);
- Assert.assertEquals("Hello World!", helloBean2.getValue());
- }
评论
private String value;
例如上面表达式为:“#{‘12+10’}”、不是String、会提示类型转化错误、如何指定相对于的类型恩?
根据你的试了下 没有报错呀~~~
private String value;
例如上面表达式为:“#{‘12+10’}”、不是String、会提示类型转化错误、如何指定相对于的类型恩?
找到了这个帖子
发表评论
-
第十九章 动态URL权限控制——《跟我学Shiro》
2014-03-28 22:51 0用过Spring Security的朋友应该比较熟悉对URL ... -
第十九章 动态URL权限控制——《跟我学Shiro》
2014-03-28 22:51 0用过Spring Security的朋友应该比较熟悉对URL ... -
在应用层通过spring解决数据库读写分离
2012-11-09 07:28 3如何配置mysql数据库的主从? 单机配置mys ... -
跟我学spring3系列 word原版 下载
2012-11-03 20:39 121782《跟我学spring3系列》自发布以来得到大家的认可,非 ... -
跟我学spring3 电子书下载(完)
2012-05-03 14:23 52502感谢iteye各位网友对我的支持,在此谢过了! ... -
跟我学spring3 目录贴及电子书下载
2012-04-10 19:00 389400扫一扫,关注我的公众号 购买地址 ... -
【第十三章】 测试 之 13.3 集成测试 ——跟我学spring3
2012-03-30 07:11 2728713.3 集成测试 13.3.1 ... -
【第十三章】 测试 之 13.1 概述 13.2 单元测试 ——跟我学spring3
2012-03-28 07:46 2345213.1 概述 13.1.1 测 ... -
【第十二章】零配置 之 12.5 综合示例-积分商城 ——跟我学spring3
2012-03-27 15:13 2063112.5 综合示例 12.5.1 概述 在第十一 ... -
【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring3
2012-03-26 08:26 2971512.4 基于Java类定义Bean配置元数据 12 ... -
【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring3
2012-03-26 08:00 56712.4 基于Java类定义Bean配置元数据 12 ... -
spring培训PPT(欢迎下载)
2012-03-24 21:55 45java私塾的 spring培训的PPT 欢迎大家下载。 包括 ... -
java私塾的spring培训PPT(欢迎下载)
2012-03-22 12:41 2973java私塾的 spring培训的PPT 欢迎大家下载。 ... -
【第十二章】零配置 之 12.3 注解实现Bean定义 ——跟我学spring3
2012-03-22 08:00 2644712.3 注解实现Bean定 ... -
【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
2012-03-19 08:00 3296412.2 注解实现Bean依赖注入 12.2.1 ... -
【第十二章】零配置 之 12.1 概述 ——跟我学spring3
2012-03-19 07:59 2007612.1 概述 12.1.1 什 ... -
【第十一章】 SSH集成开发积分商城 之 11.3 实现积分商城层 ——跟我学spring3
2012-03-16 08:09 1783511.3 实现积分商城层 11.3.1 概述 ... -
【第十一章】 SSH集成开发积分商城 之 11.2 实现通用层 ——跟我学spring3
2012-03-14 08:08 1892711.2 实现通用层 11.2 ... -
【第十一章】 SSH集成开发积分商城 之 11.1 概述 ——跟我学spring3
2012-03-13 16:37 1924111.1 概述 11.1.1 功能概述 ... -
【第十章】集成其它Web框架 之 10.4 集成JSF ——跟我学spring3
2012-03-13 08:46 12421先进行通用配置, 【第十章】集成其它Web框架 之 1 ...
相关推荐
【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3 【第六章】 AOP 之 6.1 AOP基础 ——跟我学spring3 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 【第六章】 AOP 之 6.3 基于...
Spring Expression Language (SpEL)中文文档。基于Spring4.x。
1.17 【第五章】Spring表达式语言 之 5.4在Bean定义中使用EL—跟我学spring3 . . . . . . . . . . . . . . . .197 1.18 【第六章】 AOP 之 6.1 AOP基础 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . ....
spring 表达式
—— 5.1 概述 5.2 SpEL基础5.3 SpEL语法5.4在Bean定义中使用EL6.1 AOP基础6.2 AOP的HelloWorld6.3 基于Schema的AOP6.4 基于@AspectJ的AOP 6.5 AspectJ切入点语法详解6.6 通知参数6.7 通知顺序6.8 切面实例化模型
【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring3 【第十二章】零配置 之 12.5 综合示例-积分商城 ——跟我学spring3 【第十三章】 测试 之 13.1 概述 13.2 单元测试 ——跟我学spring3 ...
Spring 中文开发手册 spring中文API Spring_表达式语言 Spring+3+中文参考手册
spring3.0.2的api文档以及spring表达式语言
spring2.5向spring3过渡,必学零配置注解实现Bean定义。
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
spring动态向容器中添加bean和删除指定bean,不需要重启应用
EL表达式 (详解) EL表达式 1、EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据。 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要使用 ...
主要介绍了Spring实战之Bean定义中的SpEL表达式语言支持操作,结合实例形式分析了Bean定义中的SpEL表达式语言操作步骤与实现技巧,需要的朋友可以参考下
spring运行过程中动态注册bean,代码实例
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
NULL 博文链接:https://conkeyn.iteye.com/blog/2354644
跟我学spring3 跟我学spring3 跟我学spring3
11.2 实现通用层 11.3 实现积分商城层 12.1 概述 12.2 注解实现Bean依赖注入 12.3 注解实现Bean定义 12.4 基于Java类定义Bean配置元数据 12.5 综合示例-积分商城 13.1 概述 13.2 单元测试 集成测试 ——跟我学spring3