springboot clever use notes

First, I referred to this blog post.

https://blog.csdn.net/valada/article/details/80892573

What is Spring Boot

The official statement is:

Spring Boot is a new framework based on Spring provided by the Pivotal team. Its design purpose is to simplify the construction and development process of Spring applications. The framework follows the "convention is greater than configuration" principle and uses a specific way to configure, so that developers do not need to define a large number of XML configuration. In this way, Spring Boot is committed to becoming a leader in the booming rapid application development field

Because individuals have been using Spring's framework for 14 years, before Spring was born, they knew that the configuration file was complicated and tiring.

In a word: springboot liberates our configuration file and focuses more time on business implementation. Simplified project construction, built-in tomcat.

In addition to the above advantages, springboot has other advantages, see below.

1. Database configuration, file upload size limit, etc., can be set through application.properties.

2. It is convenient for our packing operation, this is the key point.

   Because I used to package multiple configuration settings in application.properties, manually comment or let go to achieve the purpose, easy to make mistakes. For example, there is a development environment, a test environment, and a production environment. Then the configuration of different configuration files according to the published server is different. So this is the point.

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境名字,比如:

application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

 

For example, the following is mine.

Just write the different environment configuration in each configuration file.

3. Global variables can be set

@Value

Under normal circumstances, we need to define some global variables. The way we will think of is to define a public static variable and call it when needed. Are there other better solutions? The answer is yes. as follows:

@Value("${spring.profiles.active}")    
String active;

spring.profiles.active is defined in application.properties inside our property, we can customize any attribute name, by @Valueannotation can be taken out.

Its benefits are as follows:

    1.定义在配置文件里,变量发生变化,无需修改代码。
    2.变量交给Spring来管理,性能更好。

4.springboot can manage interceptors

Create an interceptor class: ApiInterceptor, and implement the HandlerInterceptor interface

public class ApiInterceptor implements HandlerInterceptor {
    //请求之前    
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("进入拦截器");
        return true;
    }

    //请求时   
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    //请求完成    
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

@SpringBootConfiguration The annotated class inherits the WebMvcConfigurationSupport class and rewrites the addInterceptors method to add the ApiInterceptor interceptor class. The code is as follows:

@SpringBootConfigurationpublic
class WebConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
        registry.addInterceptor(new ApiInterceptor());
    }
}

The framework of the springboot interceptor has been written, and the specific business implementation will make up the brain by itself.

 

5. Exception handling

Can refer to:

https://www.cnblogs.com/xuwujing/p/10933082.html

The SpringBoot project has dealt with certain exceptions, but it may not be suitable for our developers, so we need to capture and handle these exceptions uniformly. There is an ControllerAdviceannotation in SpringBoot . Using this annotation means that the capture of global exceptions is enabled. We only need to use a ExceptionHandlerannotation in a custom method and then define the type of captured exceptions to handle these captured exceptions uniformly.


@ControllerAdvice //全局异常补获
public class MyExceptionHandler {

    @ExceptionHandler(value =Exception.class)
    public String exceptionHandler(Exception e){
        System.out.println("未知异常!原因是:"+e);
        return e.getMessage();
    }
}

I wo n’t go into details here. You can refer to this blogger ’s article, which is very good.

6. Unit Testing

@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestDB {
    @Test
    public void test() {
    }
}

The above is just a simple framework for unit testing, please refer to:

https://blog.csdn.net/sz85850597/article/details/80427408

You're done here.

 


 

Published 51 original articles · Like 4 · Visitor 7901

Guess you like

Origin blog.csdn.net/u012174809/article/details/103023726