December 12, 2013, Spring released the 4.0 version. This was originally a repository for control reversal containers on the Java platform, and after nearly 10 years of development it has become a Big Mac product. However, it relies on a good layered design, each function module can maintain a good independence, Java platform is a rare and useful open source application framework. The 4.0 version of spring can be said to be a major update, with full support for JAVA8 and good support for the groovy language. In addition, a lot of new projects have been introduced, such as Spring boot,spring cloud,spring websocket.
Spring because of its cumbersome configuration, once become "configuration Hell", a variety of XML, annotation configuration, dazzling, and if the error is difficult to find out why. The Spring Boot project is designed to solve the cumbersome configuration problem, maximizing the implementation of the Convention over configuration (Convention is larger than configured). Programmers familiar with Ruby on Rails (ROR framework) know that a Web application can be built in just a few simple steps with the help of Ror's scaffolding tools. Spring boot is equivalent to ROR on the Java platform.
The features of Spring boot are as follows:
Create a standalone spring application
Embedded Tomcat,jetty container, no need to deploy war packages
Simplifies maven and Gradle configuration
Automate the configuration of spring as much as possible
Practical functions directly implanted in the product environment, such as metrics, health checks and extended configurations
No code generation and XML configuration required
The current version of Spring boot is 1.2.3 and requires support from JAVA7 and spring Framework4.1.5. If you want to use it under JAVA6, you need some extra setup.
If you want to create a spring-based web app that simply prints a ' Hello world ' in the page, follow the old way before you need to create the following file:
Web. XML: Configure the use of the spring servlet, as well as the other configuration;
Spring-servlet.xml: Configure the configuration of the spring servlet;
HelloController.java:controller.
If you want to run it, you need to deploy the resulting war package to the appropriate Tomcat or jetty container, which requires a corresponding configuration.
If you use spring boot, we only need to create Hellocontroller.java.
Hellocontroller.java
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - + |
Package Hello; Import org.springframework.boot.*; Import org.springframework.boot.autoconfigure.*; Import org.springframework.stereotype.*; Import org.springframework.web.bind.annotation.*; @Controller@EnableAutoConfigurationPublic class hellocontroller { @RequestMapping("/") @ResponseBody String home() { return "Hello world!" ; } Public static void main(String[] args ) throws Exception { springapplication. Run (hellocontroller. class , args ); } }
|
Then, with the help of the plug-in provided by spring boot for maven and Gradle, it is easy to run Java-jar directly after generating the jar package.
The above mentioned spring boot support for the build tools such as Maven and Gradle is very strong. Its built-in ' Starter POM ' provides a highly encapsulated project build that maximizes the configuration of project builds. There are also plug-ins for maven and Gradle, which are packaged and run without the need to write extra scripts.
Spring boot not only simplifies web applications, but also provides a range of dependencies to make other jobs out of the box. Several classic spring boot dependent libraries are listed below.
Spring-boot-starter-web: Support for full stack web development, including Tomcat and SPRING-WEBMVC.
Spring-boot-starter-mail: Provides support for Javax.mail.
SPRING-BOOT-STARTER-WS: Providing support for spring WEB services
Spring-boot-starter-test: Provides support for commonly used test frameworks, including Junit,hamcrest and Mockito.
Spring-boot-starter-actuator: Supports some functions in the product environment, such as metrics and monitoring.
Spring-boot-starter-jetty: Supports jetty containers.
- SPRING-BOOT-STARTER-LOG4J: Introducing the default log frame (Logback)
Spring Boot offers more Starter than this, please refer to the Documentation: Starter poms section for details.
If you don't like Maven or gradle,spring provide the CLI (Command line Interface) to develop the run spring application. You can use it to run groovy scripts and even write custom commands. There are several ways to install the spring CLI, see: Installing the Spring Boot CLI chapter.
You can run srping version
to view the current version after the installation is complete.
You can use groovy to write a controller.
Hello.groovy
1 2 3 4 5 6 7 8 9 |
@RestControllerclass WebApplication { @RequestMapping("/") String home() { "Hello World!" }}
|
Then use spring run hello.groovy
it to run it directly. You can see the printed information by visiting localhost:8080.
Spring Boot provides many features, such as support for MVC, external properties injection, log framework support, and more. There is no more detail here. Interested can view their documentation for detailed information.
If you want to use spring in your project, it's a good idea to set spring boot as standard because it's really developed, but you also need to read its documentation carefully to avoid falling into the pits.
The spring Boot that is worth using