目录
Spring4.1新特性——页面自动化测试框架Spring MVC Test HtmlUnit简介
Spring 4.1对核心部分没有很亮点的增强,主要还是一些改进和增强;本文将一一道来。
1、DirectFieldAccessor
Spring内部大量使用BeanWrapper进行属性取值赋值(setter/getter),不过到目前为止没有简单的办法去获取对象字段值;之前都是通过ReflectionUtils获取Field然后进行操作;Spring 4.1提供了DirectFieldAccessor来完成此功能:
//嵌套设置/访问对象字段数据 DirectFieldAccessor accessor = new DirectFieldAccessor(bean); //如果嵌套对象为null,字段创建 accessor.setAutoGrowNestedPaths(true); //设置字段值 accessor.setPropertyValue("bean2.name", "zhangsan"); //读取字段值 System.out.println(accessor.getPropertyValue("bean2.name"));
在Spring MVC中也可以通过如下方式给对象字段绑定数据:
@RequestMapping("/directField") public String directFieldInject(MyUser user) { System.out.println(user); return user.toString(); } @InitBinder public void initBinder(DataBinder dataBinder) { dataBinder.initDirectFieldAccess();//直接字段访问 必须加这句才可以 }
2、Yaml支持
YAML类似于JSON,做配置文件还是不错的,需要添加org.yaml.snakeyaml依赖。目前支持把YAML文本转换成Map和Properties:
yaml.txt
env: one: name: zhangsan two: - a: 1 b: 2 - c: "3" d: 4 three:
yaml-override.txt
env: three: 11
配置文件
<bean id="yamlMap" class="org.springframework.beans.factory.config.YamlMapFactoryBean"> <property name="resources"> <list> <value>classpath:yaml.txt</value> <value>classpath:yaml-override.txt</value> </list> </property> <property name="resolutionMethod" value="FIRST_FOUND"/> </bean> <bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean"> <property name="resources"> <list> <value>classpath:yaml.txt</value> <value>classpath:yaml-override.txt</value> </list> </property> <property name="resolutionMethod" value="FIRST_FOUND"/> </bean>
在应用中使用:
@Autowired private ApplicationContext ctx; @Autowired @Qualifier("yamlProperties") private Properties yamlProperties; @Test public void testYmlMap() { //Map(不能直接注入@Autowired Map) //请参考 Map依赖注入(http://jinnianshilongnian.iteye.com/blog/1989379) System.out.println(this.yamlMap); Map<String, Object> yamlMap = ctx.getBean("yamlMap", Map.class); //需要snakeyaml 该功能是从spring-boot引入的 Map<String, Object> env = (Map<String, Object>) yamlMap.get("env"); Map<String, Object> one = (Map<String, Object>) env.get("one"); Assert.assertEquals("zhangsan", one.get("name")); List<Map<String, Object>> two = (List) env.get("two"); Assert.assertEquals(1, two.get(0).get("a")); Assert.assertEquals("3", two.get(1).get("c")); Assert.assertEquals(null, env.get("three")); //Properties Assert.assertEquals("zhangsan", yamlProperties.getProperty("env.one.name")); //getProperty如果返回的数据时非String的则返回null Assert.assertEquals(1, yamlProperties.get("env.two[0].a")); Assert.assertEquals("3", yamlProperties.getProperty("env.two[1].c")); Assert.assertEquals("", yamlProperties.getProperty("env.three")); //spring.profiles //http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-yaml }
该特性是从Spring Boot引入,可以直接参考其文档。
此处需要注意不能:
@Autowired @Qualifier("yamlMap") private Map<String, Object> yamlMap;
原因请参考 Map依赖注入(http://jinnianshilongnian.iteye.com/blog/1989379)。
3、SmartInitializingSingleton
所有非lazy单例Bean实例化完成后的回调方法:
public class MySmartInitializingSingleton implements SmartInitializingSingleton { //所有非lazy单例Bean实例化完成后,会调用该方法 @Override public void afterSingletonsInstantiated() { System.out.println("单例Bean实例化完成了"); } }
4、Base64Utils
其会自动根据反射来决定是使用Java 8 的 java.util.Base64还是Apache Commons Codec的org.apache.commons.codec.binary.Base64。
@Test public void test() { //需要Java 8 或 Apache Commons Codec String str = "123"; String str2 = new String(Base64Utils.decodeFromString(Base64Utils.encodeToString(str.getBytes()))); Assert.assertEquals(str, str2); }
5、SpEL编译模式
SpEL支持把解释型的SpEL转换为字节码进行编译模式下执行:
@Test public void test() { SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.MIXED, getClass().getClassLoader()); SpelExpressionParser parser = new SpelExpressionParser(configuration); SpelExpression expression = parser.parseRaw("new String('haha')"); Assert.assertEquals("haha", expression.getValue()); //人工编译 //或者使用expression.compileExpression(); SpelCompiler spelCompiler = SpelCompiler.getCompiler(getClass().getClassLoader()); CompiledExpression compiledExpression = spelCompiler.compile((SpelNodeImpl) expression.getAST()); Assert.assertEquals("haha", compiledExpression.getValue(null, null)); }
SpelCompilerMode.MIXED指定了是混合模式,其在解释型和编译型之间转换, 其编译规则是这样的:
private void checkCompile(ExpressionState expressionState) { this.interpretedCount++; SpelCompilerMode compilerMode = expressionState.getConfiguration().getCompilerMode(); if (compilerMode != SpelCompilerMode.OFF) { if (compilerMode == SpelCompilerMode.IMMEDIATE) {//如果立即编译,则直接编译 if (this.interpretedCount > 1) {//只有当解释的次数大于1时才编译 compileExpression(); } } else { // compilerMode = SpelCompilerMode.MIXED if (this.interpretedCount > INTERPRETED_COUNT_THRESHOLD) {//混合模式下,需要解释次数达到阀值(默认100)才会编译 compileExpression(); } } } }
默认情况下,编译模式是禁用的,想要全局开启可以通过配置classpath:spring.properties配置文件,然后SpelParserConfiguration会在类加载时读取spring.expression.compiler.mode属性来配置:
private static final SpelCompilerMode defaultCompilerMode; static { String compilerMode = SpringProperties.getProperty("spring.expression.compiler.mode"); defaultCompilerMode = (compilerMode != null ? SpelCompilerMode.valueOf(compilerMode.toUpperCase()) : SpelCompilerMode.OFF); }
5、BackOff退避算法实现
在如连接网络的应用中,网络是不稳定的有时候会连接断开,因此为了保证断开重连接;还有如系统之间互联,相互之间发生消息,如果某个服务器因为不确定因此连接不上,也需要断开重连;则需要一定的规则;常见的规则有:
1、按照固定时间间隔重试,比如100毫秒;这种方式在网络不稳定时重连可能造成某一时间点流量同时发送,阻塞网络;或者造成发送一些无意义的请求;
2、按照指数时间间隔重试,比如刚开始100毫秒,下一次200毫秒等;比如支付宝和第三方集成时就是类似方式。
固定时间间隔重试:
@Test public void testFixedBackOff() { long interval = 100; long maxAttempts = 10; BackOff backOff = new FixedBackOff(interval, maxAttempts); BackOffExecution execution = backOff.start(); for(int i = 1; i <= 10; i++) { //每次重试时间是100毫秒 System.out.println(execution.nextBackOff()); } Assert.assertEquals(BackOffExecution.STOP, execution.nextBackOff()); }
interval是重试间隔,maxAttempts是最大重试次数,如果重试到了maxAttempts,则execution.nextBackOff()=BackOffExecution.STOP。
指数时间间隔重试:
@Test public void testExponentialBackOff() { long initialInterval = 100;//初始间隔 long maxInterval = 5 * 1000L;//最大间隔 long maxElapsedTime = 50 * 1000L;//最大时间间隔 double multiplier = 1.5;//递增倍数(即下次间隔是上次的多少倍) ExponentialBackOff backOff = new ExponentialBackOff(initialInterval, multiplier); backOff.setMaxInterval(maxInterval); //currentElapsedTime = interval1 + interval2 + ... + intervalN; backOff.setMaxElapsedTime(maxElapsedTime); BackOffExecution execution = backOff.start(); for(int i = 1; i <= 18; i++) { System.out.println(execution.nextBackOff()); } Assert.assertEquals(BackOffExecution.STOP, execution.nextBackOff()); }
initialInterval是初始重试间隔,maxInterval是最大的重试间隔, multiplier是递增倍数,maxElapsedTime是重试的最大时长。
Spring4新特性
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
Spring4新特性——注解、脚本、任务、MVC等其他特性改进
源码下载
https://github.com/zhangkaitao/spring4-1-showcase/tree/master/spring4.1-others
https://github.com/zhangkaitao/spring4-1-showcase/tree/master/spring4.1-mvc
相关推荐
【第一章】 Spring概述 ——跟我学Spring3 【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3 【第二章】 IoC 之 2.2 IoC 容器基本原理...【第七章】 对JDBC的支持 之 7.5 集成Spring JDBC及最佳实践 ——跟我学spring3
1.33 【第七章】 对JDBC的支持 之 7.4 Spring提供的其它帮助 ——跟我学spring3【私塾在线原 创】 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...
4.1 Spring MVC 概述 73 4.2 Spring MVC 项目快速搭建 74 4.2.1 点睛 74 4.2.2 示例 74 4.3 Spring MVC 的常用注解 82 4.3.1 点睛 82 4.3.2 示例 83 4.4 Spring MVC 基本配置 87 4.4.1 静态资源映射 88 4.4.2 拦截器...
4.1 Spring MVC 概述 73 4.2 Spring MVC 项目快速搭建 74 4.2.1 点睛 74 4.2.2 示例 74 4.3 Spring MVC 的常用注解 82 4.3.1 点睛 82 4.3.2 示例 83 4.4 Spring MVC 基本配置 87 4.4.1 静态资源映射 88 4.4.2 拦截器...
4.1 Spring MVC 概述 73 4.2 Spring MVC 项目快速搭建 74 4.2.1 点睛 74 4.2.2 示例 74 4.3 Spring MVC 的常用注解 82 4.3.1 点睛 82 4.3.2 示例 83 4.4 Spring MVC 基本配置 87 4.4.1 静态资源映射 88 4.4.2 拦截器...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
7.4. 使用其他过滤器—— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
7.4. 使用其他过滤器 —— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
Spring概述2.1 IoC基础2.2 IoC 容器基本原理2.3 IoC的配置使用——跟我学Spring33.1 DI的配置使用3.2 循环依赖3.3 更多DI的知识 3.4 Bean的作用域 4.1 基础知识4.2 内置Resource实现4.3 访问Resource4.4 Resource...
Spring MVC 学习总结(五)——校验与文件上传 目录 2.2.7、范围 2.2.8、其它注解 2.3、注解控制器参数 1.4、在UI中添加错误标签 1.5、测试运行 三、使用jQuery扩展插件Validate实现前端校验 3.1、jQuery扩展插件...
7.4. 使用其他过滤器 —— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...
4.1微信小程序开发流程 27 4.2项目开发方法及部署环境 29 4.3数据库设计 29 5.系统设计与实现 30 5.1 小程序——主页模块 30 5.2小程序——搜索模块 31 5.3小程序——购物车和订单模块 32 5.4小程序——个人中心模块...
———————————————— 版权声明:本文为CSDN博主「YoungJ5788」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
11.6项目实战——spring问候程序 239 本章小结 241 课后练习 241 第12章springaop 242 12.1aop基础 242 12.1.1aop与oop的比较 242 12.1.2aop的核心概念 243 12.1.3java动态代理与aop 244 12.1.4springaop...
第1篇 开发工具及框架概述 第1章 开发前奏 1.1 java web应用概述 1.2 配置开发环境 1.3 基础技术简单简介 1.4 核心框架初步认识 1.5 小结 第2章 myedipse开发工具对各种框架的支持 2.1 ...
6.1.3 spring 2.5拿手戏——控制反转与依赖注入 353 6.1.4 何为“面向切面编程aop” 356 6.1.5 spring 2.5圣经——面向接口编程 358 6.1.6 开始spring 2.5旅程—hello world 359 6.2 spring 2.5核心技术 364 ...
• 扩展性 —— Webx 3.0对Spring做了扩展,使Spring Bean不再是“bean”,而是升级成“组件”。一个组件可以扩展另一个组件,也可以被其它组件扩展。这种机制造就了Webx的非常好的扩展性,且比未经扩展的Spring更易...
16.2 实例——用Spring来打招呼 246 16.3 小结 248 第17章 JNDI和Java RMI远程调用 249 17.1 用Java RMI实现远程调用 250 17.2 利用JNDI定位资源 256 17.3 实例——分布式的HelloWorld 260 17.4 小结 ...