sprint boot

1. What is sprint boot

A new framework provided by the Pivotal team, designed to simplify the initial setup and development of new Spring applications. The framework is configured in a specific way so that developers no longer need to define boilerplate configurations. (Sprint boot is not actually a new framework. It configures the use of many frameworks by default, just like maven integrates all jar packages, and springboot integrates all frameworks.)

 

Due to the complex configuration of spring, various xml, annotation annotations, etc., it is difficult to locate complex errors. sprintboot is to solve the complex and maximize the prior convention over configuraion (convention is greater than configuration)

characteristic:

1. The spring application of chaungjainduli;

2. Embedded Tomcat, Jetty and other containers are not allowed to be war packages

3. Simplify maven and gradle configuration

4. Automatically configure spring as much as possible

5. Practical functions directly embedded in the product environment, metrics, health checks and extended configurations.

6. No code generation and XML configuration required

 

2. Advantages: simple and convenient

1. The configuration method of pure JAVA is very simple and convenient
. 2. With various starters, it can basically achieve automatic configuration
.

shortcoming

1. The automatic configuration causes some things that don't apply to what you think, you have to go to the javaconfig file to overwrite the original default configuration
2. If you want to know more about springboot, you need to know more about relevant documents and source code

A traditional application springweb starts:

1. Configure web.xml, load spring and springMVC

2. Configure database connection, configure spring transaction

3. Configure the reading of the loading configuration file and enable annotations

4. Configure the log file

After the configuration is complete, the tomcat deployment starts

 

For the very popular microservices, this back and forth takes a lot of time and energy to repeat the work.

 

Springboot can quickly build a set of web projects and build a microservice with very little time and effort and simple configuration.

 

spring boot project structure:

with 

 -project

  --Application.java

  -domain

   --User.java

   --UserDao.java

  -service

   --UserService.java

  -controller

   --UserController.java

1.Application.java is stored in the project root directory, configuration framework, main method

2.domain stores entity and data access layer

3. service layer business layer

4.controller control layer

 

pom.xml file:

1. Add web module

<dependency>

 <groupId>org.springframework.boot</groupId>

 <artifactId>spring-boot-starter-web</artifactId>

</dependency>

pom.xml has two default modules by default:

spring-boot-starter: core module, including auto-configuration support, logging and YAML

spring-boot-starter-test: test modules, Jnuit, Hamcrest, MockIto

 

spring-boot-starter-web provides us with the most basic embedded Tomcat and SpringMVC dependencies, in addition to template engines such as FreeMarker, Groovy, Thymeleaf. Velocity and Mustache. It is recommended to use Thymeleaf. During use, configure the beans required for inheritance through the ThymeleafAutoConfiguration class.

 

In addition to the most basic web simplified packaging, it also provides a series of reports to make other work out of the box.

spring-boot-starter-web: web development, tomcat, springMVC and template engine

spring-boot-starter-mail:javax.main支持;

spring-boot-starter-ws:webservice

spring-boot-starter-test: test cases

spring-boot-starter-actuator: functions, health checks, metrics, etc. in the production environment

spring-boot-starter-jetty: (tomcat) inline container;

spring-boot-starter-log4j: logback logging framework

 

 

2. Write the controller content:

@RestController

public class HelloController{

 @RequestMapping(/hello)

 public String index(){

  return "hello";

 }

}

The meaning of @RestController is that the methods in the controller are all output in json format, and there is no need to configure the json output format.

 

3. Unit testing

Open the src/test test entry, write a simple http request to test; use mockmvc to do it, and use MockMvcResultHandlers.print() to print the execution result.

 

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = MockServletContext.class)

@WebAppConfiguration

public class HelloControllerTest{

 private MockMvc mvc

 @Before

 public void setUp throws Exception{

  mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();

 }

 

 @Test

 public void testHello() throws Exception{

 mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON).andExpect( MockMvcResultMatches.status().isOK()).andDo(MockMvcHandlers.print()).andReturn();

 }

}

 

Using sprintboot can be very convenient and fast to build a project. We don't need to care about the compatibility between the frameworks and the trial version. We only need to add a configuration to use anything.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326227337&siteId=291194637