Springboot 重点知识点最强整理

目录

1、springboot controller 单例

2、Springboot 环境变量配置

3、restful 接口

4、@ResponseBody 和 @RequestBody 的区别

5、@Schedule

6、async异步调用

7、request 的获取方式和线程安全


1、springboot controller 单例

Spring中 controller默认是单例的,因为单例所以不是线程安全的。

所以需要注意下面几点

  1. 不要在Controller中定义成员变量,可能会造成数据混乱。
  2. 万一必须要定义一个非静态成员变量时候,则通过注解@Scope(“prototype”),将其设置为多例模式。
  3. 在Controller中使用ThreadLocal变量解决多线程问题

spring bean作用域:

singleton:单例模式,当spring创建applicationContext容器的时候,spring会欲初始化所有的该作用域实例,加上lazy-init就可以避免预处理;

prototype:原型模式,每次通过getBean获取该bean就会新产生一个实例,创建后spring将不再对其管理;

2、Springboot 环境变量配置

在开发的过程中不是所有的配置文件都可以写入yaml或者properties文件中,可能需要动态的从容器中读取,看下如何配置

现在看看如果我们使用环境变量的方式来配置我们的参数,如下:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: ${DEMO_APP_JDBC_URL:jdbc:mysql://127.0.0.1/demo?serverTimezone=Asia/Shanghai}
    username: ${DEMO_APP_JDBC_USERNAME:demo}
    password: ${DEMO_APP_JDBC_PASSWORD:demo}
    hikari:
      maximum-pool-size: ${DEMO_APP_JDBC_POOL_MAX_SIZE:30}
      max-lifetime: ${DEMO_APP_JDBC_POOL_MAX_LIFE_TIME:60000}
      minimum-idle: ${DEMO_APP_JDBC_POOL_MIN_SIZE:5}

以上使用${ENV:defauleValue}的形式配置了我们应用的相关参数,如果我们的运行环境配置了上面用到的环境变量,则使用环境变量中的配置,如果没有配置则使用默认的,比如我配置了环境变量DEMO_APP_JDBC_POOL_MAX_SIZE = 100,则应用程序中的连接池最大连接数就变成100了。

配合docker 食用更香哦

3、restful 接口

现在的应用基本上都是前后端分离的,所以后端接口基本上都是restful接口,怎么做呐?

在controller 的class 上增加注解 @RestController,就会生成restful接口

@RestController的作用 相当于@Controller + @ResponseBody

@RestController
@RequestMapping("/api/v1/h5Adapter")
@Api(description = "server-h5-adapter")
public class BaMessageConverterController {

4、@ResponseBody 和 @RequestBody 的区别

@ResponseBody是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用【也就是AJAX】,在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。

@RequestBody是作用在形参列表上,用于将前台发送过来固定格式的数据【xml 格式或者 json等】封装为对应的 JavaBean 对象,封装时使用到的一个对象是系统默认配置的 HttpMessageConverter进行解析,然后封装到形参上。

5、@Schedule

定时任务几乎是项目的标配了, SpringBoot内置了Sping Schedule定时框架,通过注解驱动方式添加所注解方法到定时任务,根据配置定时信息定时执行

主要是以下两步:

1、开启定时器

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker
@EnableCaching
@EnableScheduling
public class Application {
​
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
​

2、在方法上加上注解

    //每天凌晨1点执行
    @Scheduled(cron = "0 0 1 * * ?")
    public void doTask() {
    
    }

6、async异步调用

异步调用主要处理一些不要求及时返回的任务,springboot提供了支持

SpringBoot中使用 async实现异步调用

基于注解的使用方式包括如下三步:

  1. 启动类加上@EnableAsync(也可以在配置类中加上)

  2. 配置类中完成异步线程池的导入(这一个可以不要,采用默认的)

  3. 需要异步调用的方法加上@Async

定义线程池

package com.aispeech.bj.bams.msproduct.config;
​
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
​
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
​
@Configuration
@EnableAsync(mode = AdviceMode.ASPECTJ)
public class ExecutorConfig {
​
    /** Set the ThreadPoolExecutor's core pool size. */
    private int corePoolSize = 10;
    /** Set the ThreadPoolExecutor's maximum pool size. */
    private int maxPoolSize = 300;
    /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
    private int queueCapacity = 10;
​
    @Bean
    public Executor myAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("MyExecutor-");
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
​
}
​

使用异步 @Async 注解

  @Async("myAsync")
    void notifyProductInfo(String thirdPartyId, ThirdPartyProductVO thirdPartyProductVO) {}

7、request 的获取方式和线程安全

request 是来自前端的请求,怎么才能安全的获取request,线程安全的request 才能取出正确的数据,主要有下面几种方式

1、静态类获取

HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

2、函数参数

Controller中获取request对象后,如果要在其他方法中(如service方法、工具类方法等)使用request对象,需要在调用这些方法时将request对象作为参数传入

此时request对象是方法参数,相当于局部变量,毫无疑问是线程安全的。

@RequestMapping("/test")
    public void test(HttpServletRequest request) {
        ......
    }

3、自动注入

    @Autowired
    private HttpServletRequest request; //自动注入request

对象是线程局部变量(ThreadLocal),因此request对象也是线程局部变量;这就保证了request对象的线程安全性。

不会有线程安全问题的 实际上spring 注入的是一个代理类

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>