Spring Framework 4.0 GA Release是Spring框架的一个重要版本,它引入了许多新特性和改进。以下是一些主要的变化和亮点:
-
Java 8支持:Spring 4.0全面支持Java 8,包括Lambda表达式、Stream API等新特性。这为开发者提供了更简洁、更强大的编程方式。
-
异步编程模型:Spring 4.0引入了全新的异步编程模型,使得编写非阻塞代码变得更加容易。通过使用
@Async
注解,可以方便地定义异步方法。 -
WebSocket支持:该版本增加了对WebSocket的原生支持,使得开发实时通信应用更加便捷。
-
HTTP/2支持:Spring 4.0开始支持HTTP/2协议,提高了网络传输的效率和性能。
-
简化的配置:Spring 4.0进一步简化了配置,减少了XML配置文件的使用,更多地采用注解和自动配置。
-
安全性增强:在安全方面,Spring Security进行了多项改进,增强了认证和授权功能。
-
测试框架升级:Spring TestContext Framework也进行了更新,支持更多的测试场景和工具。
-
性能优化:针对核心模块进行了性能优化,提高了整体运行效率。
-
文档和社区支持:官方文档进行了更新和完善,同时社区支持也更加活跃,帮助开发者更快地解决问题。
这些变化使得Spring Framework 4.0成为当时最受欢迎和广泛使用的Java企业级应用开发框架之一。
Spring Framework 4.0引入了许多新特性,以增强其功能和易用性。以下是一些主要的新特性:
-
Java 8支持:
- Spring 4.0完全支持Java 8的Lambda表达式、方法引用等特性,使得代码更加简洁和易读。
- 提供了对
java.util.function
包的支持,包括Supplier
,Consumer
,Predicate
等接口。
-
改进的依赖注入:
- 增强了对泛型类型的支持,允许更灵活地定义和管理bean。
- 引入了新的注解,如
@Conditional
和@Profile
,用于根据条件加载特定的bean配置。
-
Web开发增强:
- 支持嵌入式Servlet容器(如Tomcat, Jetty, Undertow),简化了Spring应用程序的开发和部署。
- 提供了新的WebSocket支持,使得构建实时通信应用更加方便。
-
测试框架改进:
- 增强了对JUnit 4的支持,并引入了对Mockito的集成,提高了单元测试的效率和可靠性。
-
异步编程支持:
- 引入了新的异步编程模型,通过
@Async
注解和TaskExecutor
接口,简化了异步任务的开发。
- 引入了新的异步编程模型,通过
-
缓存抽象:
- 提供了新的缓存抽象层,使得集成各种缓存技术(如EHCache, Redis)变得更加容易。
-
安全框架更新:
- 增强了对OAuth2和OpenID Connect的支持,使得构建安全的Web应用程序更加简便。
-
性能优化:
- 引入了新的性能优化工具,如Spring Boot Actuator,用于监控和管理Spring应用程序的性能。
-
国际化支持:
- 增强了对国际化(i18n)的支持,使得应用程序更容易本地化。
-
其他改进:
- 引入了新的配置选项,如
@ConfigurationProperties
注解,简化了外部化配置的管理。 - 提供了新的工具类和方法,如
ObjectProvider
,用于延迟加载和懒初始化bean。
- 引入了新的配置选项,如
The Spring Framework re-invented enterprise Java in the last decade, becoming the dominant programming model in enterprise Java. Today we are releasing Spring Framework 4.0, a brand new major version of Spring that keeps Spring at the cutting edge of modern Java development. Together with the rest of the upcoming Spring IO Platform, Spring Framework 4.0 is positioned to empower the next decade of JVM based innovation, responding to, and setting trends in Developer Productivity, Big Data, Cloud, REST, and Micro Service Architecture.
Spring Framework 4.0 works beautifully with Java 8 and also allows applications to be written partially or entirely in Groovy, a concise dynamic language on the JVM. Spring Framework 4.0 also provides the foundation for new advances in the Spring IO platform such as Spring Boot. Spring Boot nearly eliminates initial configuration work altogether. Developer productivity options like these are typically associated with single-purpose frameworks that are limited in scope and simply do not offer the breadth, depth, and years of battle-tested production usage that Spring offers.
A major goal of Spring IO is to make Java a leader in developer productivity via components like Spring Boot. Edward Hieatt, at the SpringOne 2GX 2013 conference keynote, had some great remarks about Boot: “The bar for developer happiness and intolerance of boilerplate code has been set very high at Pivotal Labs, which historically has done a lot of Ruby on Rails development, our Ruby on Rails developers who use Spring Framework 4.0 with Spring Boot are impressed with its productivity and ability to address a wide range of scenarios on a single programming model.” Edward Hieatt is the chief operating officer at Pivotal Labs, who built the spring.io website using Spring Framework 4.0 and Spring Boot, among other technologies.
Modernization
Spring Framework 4.0 offers 1st class Java 8 support now, based on the pre-release Java 8 builds, and will be immediately production-capable once OpenJDK 8 is GA in March 2014. Spring Framework 4.0 doesn’t just run on Java 8, it makes it really easy and natural to leverage new language features such as Lambdas, method references, JSR-310 date and time, and repeatable annotations. The framework also has been updated to work with the latest versions of popular open source libraries like Hibernate, Quartz, and EhCache.
New application architectures
Industry leaders such as Amazon, Google, Facebook and many others have already moved away from monolithic Java architecture, in favor of micro-service architecture (MSA) and REST - often moving presentation logic to the client tier. This important trend in decomposing applications, using RESTful principles, is similar to traditional SOA only in concept, but we agree with those that feel that it deserves to be called something different.
James Lewis wrote a popular InfoQ presentation where he articulated some key characteristics of a micro service that we agree with:
Each application only does one thing
Small enough to fit in your head
Small enough that you can throw them away
Embedded web container
Packaged as a single executable jar
Use HTTP and HATEOAS to decouple services
Each app exposes metrics about itself
The core Spring Framework is looking ahead into this micro-service future, with both direct and foundational support. REST is a 1st class citizen across Spring, reflected in the core 4.0 framework with Spring MVC as well as Spring IO platform components like Spring Boot, Spring HATEOAS, Spring Security, Spring Social, and Spring Data. With Spring Framework 4.0, developers can create more reactive event-driven REST services, using the new non-blocking AsyncRestTemplate together with Java language features like Futures.
Spring Boot, which builds on Spring Framework 4.0’s @Conditional bean definition infrastructure, offers a “containerless” (embedded) runtime for REST based micro services, the ability to package as a single executable JAR, in addition to other lightweight capabilities - around exposing metrics for itself, for example.
Reactive, event-driven applications are also enabled by Spring Framework 4.0’s leading support for HTML5/WebSocket RFC 6455/JSR-356. Spring’s WebSocket support provides the essential protocol fallback mechanisms that are required by today’s network topologies. More importantly, it provides a foundation for building WebSocket-style messaging architectures for use in web applications. That foundation integrates STOMP, Spring MVC, and lightweight, client side message brokers like msgs.js or full-blown server side message brokers like RabbitMQ. Also, the core message and channel abstractions from Spring Integration are now included in Spring Framework 4.0 to support server side messaging.
Debunking myths
Many who are unfamiliar with recent versions associate Spring closely with XML, but it has long offered a rich, annotation - driven programming model. This model was introduced in Spring Framework 3, and is being further refined in Spring Framework 4. Looking across both releases, the model offers capability like:
Custom annotations through composition
Configuration classes and factory methods
Spring Expression Language
Flexible MVC/REST controller style
Declarative validation and formatting
Declarative scheduling and caching
Also, Java Configuration is widely supported across Spring Projects, including the core framework, as another alternative to XML and Annotation driven configuration.
Working with the JCP
The Spring approach to Java EE support is reflected in Spring Framework 4.0’s innovations around configuration, WebSocket, DI, Batch, etc. For relevant Java EE specifications Spring collaborates in the JCP to define, and then works to exceed the related JSR specification. Pivotal has been an active collaborator in the JCP around key Java EE 7 features like WebSocket, and directly inspired the batch JSR with Spring Batch. Java EE 7 also introduced some revisions to their JMS, JTA, JPA, and Bean Validation specifications that Spring is supporting.
The evolution of Spring goes far beyond what is addressed here in the core Spring Framework. A modular micro-platform is needed to address future architectures, trends, and technologies; something to enable new features, yet retain a lightweight footprint. Spring Framework 4.0 is a critical element of the Spring IO platform. The other Spring IO platform components compliment Spring Framework 4.0 in that they address modern trends in Hadoop and NoSQL, batch processing, enterprise integration patterns, and reactive/event-driven application development. These options allow micro services to be developed with just the dependencies that are required for the job, while maintaining a consistent, but simple POJO programming model.
Spring Framework 4.0 is open source and available at no charge under an Apache 2.0 license. We encourage you to use Maven or Gradle, and coordinates can be obtained from the spring.io website.
Learning more, or getting your hands dirty
The Spring Framework project page is always a great start. But we’ve worked hard on new guides to accompany the launch of the Framework. In keeping with our philosophy of “let’s build a better enterprise” we’ve developed these getting started guides with a few principles:
Be the simplest possible example , not the “best”, as that is subjective
Use the most up-to-date Spring best practices
Do not give in to the temptation to become a full-blown tutorial, keep those separate
Make assumptions. Separate underlying concepts into linked, distinct documents
Stay task oriented, use case-oriented, explain things beyond Spring when relevant
Above all, be a resource that experts and beginners alike can appreciate
We’ve launched many new guides in celebration of the Spring Framework 4.0 release. We hope you enjoy them!
https://spring.io/guides/gs/messaging-stomp-websocket/
https://spring.io/guides/gs/consuming-rest-angularjs/
https://spring.io/guides/gs/consuming-rest-backbone/
https://spring.io/guides/gs/consuming-rest-jquery/
https://spring.io/guides/gs/consuming-rest-restjs/
https://spring.io/guides/gs/consuming-rest-sencha/
https://spring.io/guides/gs/consuming-rest-ios/
https://spring.io/guides/gs/rest-service-cors/
https://spring.io/understanding/cors
This compliments the other guides already available at spring.io, with more to come. In depth, 90-minute talks are starting to get published on our YouTube channel from SpringOne2GX 2013, as well as many 60 minute webinar replays on a variety of topics.
We hope you enjoy the release, and want to hear your feedback about how you use Spring to build a better enterprise.
comments powered by Disqus
Spring框架在过去十年中确实对Java企业级开发产生了深远的影响,它通过提供一种轻量级的、非侵入式的架构来简化企业应用的开发。以下是关于Spring框架的一些关键点:
-
控制反转(IoC)和依赖注入(DI):Spring的核心特性之一是控制反转,它允许对象之间的依赖关系由容器来管理,而不是由对象自己创建和管理。这有助于降低组件之间的耦合度,使得代码更加模块化,易于测试和维护。
-
面向切面编程(AOP):Spring支持面向切面编程,这使得开发者可以将横切关注点(如日志记录、事务管理和安全性)从业务逻辑中分离出来。通过使用AOP,可以更干净地实现这些功能,而不必在每个方法中重复编写相同的代码。
-
数据访问框架:Spring提供了多种数据访问技术的支持,包括JDBC、Hibernate、JPA等。Spring Data项目进一步简化了数据库操作,提供了一套通用的接口和实现,使得开发者可以更容易地进行数据持久化操作。
-
Web框架:Spring MVC是一个成熟的Web框架,用于构建RESTful服务和Web应用程序。它提供了模型-视图-控制器(MVC)架构的完整实现,并且与Spring的其他部分无缝集成。
-
微服务支持:随着微服务架构的兴起,Spring Cloud项目应运而生,它为构建分布式系统提供了工具集,包括服务发现、配置管理、断路器模式等。
-
测试友好:Spring的设计考虑了测试的需求,提供了丰富的测试支持,包括模拟(Mocking)框架的集成,使得单元测试和集成测试变得更加容易。
-
社区和生态系统:Spring拥有一个活跃的社区和一个庞大的生态系统,包括各种开源项目和商业产品,这些都为Spring用户提供了广泛的选择和支持。
Spring框架在过去十年中重新发明了企业Java,成为企业Java中占主导地位的编程模型。今天我们将发布SpringFramework4.0,这是Spring的一个全新的主要版本,它将Spring保持在现代Java开发的前沿。Spring Framework 4.0与即将推出的Spring IO平台的其他部分一起,将为下一个基于JVM的创新十年提供支持,响应并设定开发人员生产力、大数据、云、rest和微服务架构的趋势。
Spring Framework 4.0与Java 8完美地结合在一起,还允许应用程序部分或全部用Groovy编写,Groovy是JVM上一种简洁的动态语言。Spring框架4也为Spring IO平台(如Spring Bug)的新进展提供了基础。Spring Boot几乎完全消除了初始配置工作。像这样的开发人员生产力选项通常与单用途框架相关联,这些框架在范围上是有限的,并且不能提供Spring提供的广度、深度和经过多年战斗测试的产品使用。