目录
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
相关推荐
5. **Spring的未来**:尽管这些版本已经较旧,但它们仍然是许多遗留系统的核心组成部分。随着Spring的发展,新的版本如Spring 5.x和6.x带来了更多的特性,如对Java 11+的支持、响应式编程模型、Kubernetes集成等。...
- **介绍:** 对Spring框架进行了全面而深入的介绍,包括其设计理念、核心特性及应用场景。 - **知识点:** - **依赖注入与控制反转:** 解释了DI(Dependency Injection)的概念及其在Spring中的实现方式——IoC...
1.33 【第七章】 对JDBC的支持 之 7.4 Spring提供的其它帮助 ——跟我学spring3【私塾在线原 创】 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
接着,我们将深入探讨Spring的核心原理——控制反转和依赖注入。此外,我们还将学习如何在Spring框架中配置数据库JDBC,并整合SpringMVC。最后,我们将详细介绍Spring的搭建流程,包括环境搭建、项目结构设计以及...
- **依赖注入**:Spring的核心特性之一是依赖注入(DI),它使得开发者能够更加灵活地管理对象之间的依赖关系。 - **模块化设计**:Spring提供了多个模块,包括核心容器、数据访问/集成、Web、AOP、测试等,可以根据...
Spring的核心特性可以用于任何Java应用程序,但还有扩展,支持构建Web应用程序。 标题“跟我学spring”和描述“spring 的使用,每个知识点和项目中的运用,20章的介绍。”暗示这是一份详细介绍Spring框架使用方法的...
替代教程通过一个具体的例子——婚礼RSVP应用——来展示如何使用SpringRoo。这个例子包括: - 应用概述:描述应用的功能需求。 - 步骤详解:逐步指导如何使用SpringRoo完成应用开发的各个阶段。 - 最终成果:展示...
Spring框架是Java开发中的一个核心工具,它提供了丰富的功能来简化企业级应用的开发,包括与各种MQ的集成。在这个“spring+activeMQ消息队列”的主题中,我们将深入探讨Spring如何与ActiveMQ结合使用,以及相关的...
#### 一、Spring框架的核心——IoC容器 Spring框架是一个开源的轻量级Java开发框架,其核心功能是IoC(Inversion of Control,控制反转)容器和AOP(Aspect Oriented Programming,面向切面编程),这些功能大大...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
1.2 IoC(控制反转)与DI(依赖注入):Spring的核心特性之一,通过容器管理对象的生命周期和依赖关系,降低了组件间的耦合度,提高了代码的可测试性和可维护性。 1.3 AOP(面向切面编程):Spring的AOP模块允许...
SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...
这个示例应用将帮助理解Spring Boot的一些核心特性,如自动配置、依赖管理和启动速度等。 #### 重要知识点 1. **Spring Boot简介** - **Spring Boot** 是Spring平台的一个新框架,它简化了基于Spring的应用程序的...
##### 2.3 Struts2的核心——拦截器 拦截器是Struts2框架中的重要组成部分,它提供了AOP(面向切面编程)的实现方式。拦截器可以被看作是一种在Action前后执行的代码片段,它允许开发者在Action执行前后添加自定义...
Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架、REST风格的Web编程模型等。这些新功能实用性强、易用性高,可大幅降低Java应用,特别是JavaWeb应用开发的难度,同时有效提升...
基于Spring Boot的网上购物系统设计与实现是一项重要的技术实践,它结合了现代Web开发的两大主流框架——Spring Boot和Vue.js,构建了一个高效、灵活且易于维护的电商平台。本论文详细探讨了如何利用这些技术来构建...
这部分将指导学员如何将Spring与Hibernate进行集成,实现事务的统一管理、资源的统一配置,构建一个更为健壮和高效的数据访问层。 #### 四、Struts2+Hibernate+Spring网上购物系统开发 **4.1 网上购物系统设计** ...
### Spring 3.x企业应用开发实战之实战案例开发——论坛应用 #### 1. 案例概述 在本章中,《Spring 3.x企业应用开发实战》一书通过一个具体的论坛案例,深入介绍了如何使用Spring+Hibernate集成框架进行开发的过程...
尤其适用于使用领先的J2EE解决方案——Spring框架开发的企业软件项目。 如果你还没有使用Spring开发企业软件,建议你先学习Spring框架,特别是依赖注入(Dependency Injection, DI)原理。熟悉Spring框架将有助于你...
7.4. 使用其他过滤器 —— 基于框架 8. 核心安全过滤器 8.1. FilterSecurityInterceptor 8.2. ExceptionTranslationFilter 8.2.1. AuthenticationEntryPoint 8.2.2. AccessDeniedHandler 8.3. ...