Brief introduction of new features of Spring4

Source: Internet
Author: User

Spring is an extremely popular open source framework in the Java world. Spring's original intention was to reduce the complexity of enterprise-level development, and to attempt to implement the functionality of a heavy framework such as EJB prior to the Pojo object. Spring is not only useful for server-side development, but any Java application can benefit from the simplicity, ease of testing, and low coupling of spring.

The spring framework uses beans or JavaBeans to represent components in an application, but this does not mean that the component must strictly meet the specifications of the Java bean.
Spring does a lot of things, but in the end there are some basic ideas, and all of these ideas ultimately lead to Spring's mission: simplifying Java development .
Spring simplifies Java development with the following four strategies:
Lightweight, minimal intrusive development based on Pojo;
Loose coupling is achieved by dependency injection and interface-oriented programming;
Declarative programming is achieved through aspect-oriented programming and conventions;
Eliminate boilerplate code with aspect-oriented programming and templates
By functional partitioning, these modules can be divided into six groups, as shown in:

These modules can meet almost all enterprise application development needs, but developers do not need to fully use these spring modules, and the freedom to choose a third-party module that meets the requirements of the project--spring provides interactive interfaces for some third-party modules.
CORE SPRING CONTAINER
The core module of the spring framework, and all other modules are built on that module. The spring container is responsible for managing the creation, configuration, and management of beans in spring applications. In this module, Spring Bean Factory, which provides the most basic dependency injection (DI) functionality, is based on Bean factory, which provides an implementation of the central spring application context that can be chosen by the developer.
In addition to the Bean factory and application context, the module also supports other enterprise-class services such as email, JNDI access, EJB integration, and scheduling.
SPRING ' s AOP MODULE
The spring framework provides aspect-oriented programming capabilities through an AOP module. With AOP modules, some system-level requirements (transactions, security) can be decoupled from the modules they really want to work with.
DATA ACCESS and integration
Spring's JDBC andData-access ObjectModules encapsulate some boilerplate code for database operations, eliminating a lot of effort from developers. The module also encapsulates exceptions at the database layer and provides more meaningful exception information upwards.
Spring does not implement its own ORM framework, but it provides the ability to integrate with several other ORM frameworks, such as Hibernate, Mybatis, Java persistence APs, and so on, and these ORM frameworks support the use of the transaction management module provided by spring.
WEB and REMOTING
Spring provides its own web development framework,--spring MVC, which also provides remote invocation support: Invocation (RMI), Hessian, Burlap, and Jax-ws.
Instrumentation
Less frequently used
Testing
Can be used in combination with common JUnit, Mockito, Spock and other test frameworks.
Spring4 new Features
spring4.x not only supports JAVA8, but is backward-compatible to Javase6/javaee6, and removes related discarded classes, new additions such as Java8 support, groovy bean definition DSLs, enhancements to the core container, enhancements to the web framework, WebSocket module implementation, test enhancement and so on.
Here is a simple list of places that are actually used much more:
Support Map,list Injection
@Autowired
Private map<string,myservice> Map;
@Autowired

Private list<myservice> List;

for list, all myservice types of beans are injected
Class proxy based on Cglib no longer requires that the class must have an empty parameter constructor:
This is a good feature, there are many benefits of using constructor injection, such as the ability to inject dependencies only when the bean is created, and then not change, if using setter injection, is allowed to change.
Web Development enhancements
to facilitate rest development, the new @restcontroller is specified on the controller so that there is no need to add @responsebody on each @requestmapping method. It also adds a asyncresttemplate that supports asynchronous nonblocking support for rest clients.

@RestController
public class Usercontroller {
Private UserService UserService;
@Autowired
Public Usercontroller (UserService userservice) {
This.userservice = UserService;
}
@RequestMapping ("/test")
Public User view () {
User user = new user ();
User.setid (1L);
User.setname ("haha");

return user;
}
}

Provides asyncresttemplate for client-side non-blocking asynchronous support.
Server-side

@RestController
Public Classusercontroller {
Private UserService UserService;
@Autowired
Publicusercontroller (UserService userservice) {
This.userservice = UserService;
}
@RequestMapping ("/api")
Publiccallable API () {
System.out.println ("=====hello");
return new callable () {
@Override
Publicuser call () throwsexception {
Thread.Sleep (10L *1000);//pause for two seconds
User User =new User ();
User.setid (1L);
User.setname ("haha");
return user;
}
};
}
}

Client

public static void Main (string[] args) {
Asyncresttemplate template =new asyncresttemplate ();
Returns immediately after the call is complete (no blocking)
listenable<future> future= template.getforentity ("Http://localhost:9080/spring4/api", User.class);
Set asynchronous callbacks
Future.addcallback (New listenablefuturecallback> () {
@Override
public void onsuccess (responseentity result) {
System.out.println ("======client Get Result:" + result.getbody ());
}
@Override
public void OnFailure (Throwable t) {
System.out.println ("======client failure:" + t);
}
});
System.out.println ("==no Wait");
}

The future is used here to do non-blocking, so we also need to give it a callback interface to get the results; the future and the callable are a pair, a consumption result, a result. When the template is finished, it returns immediately, does not block, and invokes its callback when there is a result.
new features of Bean validate
Supports cross-parameter validation (such as password and Confirm password validation) and the use of El Expressions in messages, as well as method parameter/return value validation, CDI and dependency injection, grouping transformations, and more.
Some of the other improvements:
1.The core parts are basically unchanged, providing directfieldaccessor for direct field access, YAML configuration, Spel bytecode compilation, Backoff Backoff algorithm, Base64utils, Smartinitializingsingleton, etc.;
2.The exception processing part is added to the task scheduling and event mechanism.
3.The cache part joins Jcache's integration, class-level @cacheconfig support, cacheresolver;
4.The MVC section provides a simplified configuration of the MVC tags for some view parsers, provides groovywebapplicationcontext for groovy Web integration, provides Gson, Protobuf Httpmessageconverter, Static resource processing adds resolver and transformer, provides support for groovy-templates templates, JSONP support, and support for Jackson's @jsonview.
5.Provides a page Automation test framework for Spring MVC test htmlunit;
6.The test section provides a more convenient @sql tag to perform the initialization of the test script, Mockrestserviceserver to ayncresttemplate support, mockmvcconfigurer to the global configuration Mockmvc;
7.Provides support for JAVA8 optional (objecttooptionalconverter implementation; You can bind to annotations such as @requestparam in MVC), Ehcache support from Spring 4.1 requires Ehcache 2.5 or higher, compatible with quartz 2.1.4, support for javax.annotation.Priority (equivalent to @order), enhancements to JMS APIs, Springjunit4classrunner requires JUnit 4.9 or later version support.
Overall there is no big change, just some enhancements, so it's easy for us to upgrade.

Brief introduction of new features of Spring4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.