玩转Spring,你不得不知的实用功能

一、实现初bean的始化

spring中支持3种初始化bean的方法:

  • xml中指定init-method方法
  • 使用@PostConstruct注解
  • 实现InitializingBean接口

第一种方法太古老了,现在用的人不多,具体用法就不介绍了。

1.使用@PostConstruct注解

在需要初始化的方法上增加@PostConstruct注解,这样就有初始化的能力。

@Service
public  class TestService1 {

    @PostConstruct
    public void init() {
        System.out.println("===TestService1初始化===");
    }
}
2.实现InitializingBean接口

实现InitializingBean接口,重写afterPropertiesSet方法,该方法中可以完成初始化功能。

@Service
public  class TestService2 implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("===TestService2初始化===");
    }
}

疑问:init-method、PostConstruct 和 InitializingBean 的执行顺序是什么样的?

决定他们调用顺序的关键代码在AbstractAutowireCapableBeanFactory类的initializeBean方法中。
在这里插入图片描述
这段代码中会先调用BeanPostProcessorpostProcessBeforeInitialization方法,而PostConstruct是通过InitDestroyAnnotationBeanPostProcessor实现的,它就是一个BeanPostProcessor,所以PostConstruct先执行。

invokeInitMethods方法中的代码:
在这里插入图片描述
决定了先调用InitializingBean,再调用init-method。

所以得出结论,他们的调用顺序是:
在这里插入图片描述

二、获取spring容器对象

1.实现ApplicationContextAware接口

最常用的是方法是:实现ApplicationContextAware接口,然后重写setApplicationContext方法,也能从该方法中获取到spring容器对象。

@Service
public  class PersonService1 implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void add() {
        Person person = (Person) applicationContext.getBean("person");
    }

}
2.实现BeanFactoryAware接口

实现BeanFactoryAware接口,然后重写setBeanFactory方法,就能从该方法中获取到spring容器对象。

@Service
public  class PersonService2 implements BeanFactoryAware {
    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    public void add() {
        Person person = (Person) beanFactory.getBean("person");
    }
}
3.实现ApplicationListener接口

实现ApplicationListener接口,需要注意的是该接口接收的泛型是ContextRefreshedEvent类,然后重写onApplicationEvent方法,也能从该方法中获取到spring容器对象。

@Service
public  class PersonService3 implements ApplicationListener<ContextRefreshedEvent> {
    private ApplicationContext applicationContext;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        applicationContext = event.getApplicationContext();
    }

    public void add() {
        Person person = (Person) applicationContext.getBean("person");
    }

}

此外,不得不提一下Aware接口,它其实是一个空接口,里面不包含任何方法。

它表示已感知的意思,通过这类接口可以获取指定对象,比如:

  • 通过BeanFactoryAware获取BeanFactory
  • 通过ApplicationContextAware获取ApplicationContext
  • 通过BeanNameAware获取BeanName等

Aware接口是很常用的功能,目前包含如下功能:
在这里插入图片描述

三、spring mvc拦截器

spring mvc拦截器根spring拦截器相比,它里面能够获取HttpServletRequest和HttpServletResponse 等web对象实例。

spring mvc拦截器的顶层接口是:HandlerInterceptor,包含三个方法:

  • preHandle 目标方法执行前执行
  • postHandle 目标方法执行后执行
  • afterCompletion 请求完成时执行

为了方便我们一般情况会用HandlerInterceptor接口的实现类HandlerInterceptorAdapter类。

假如有权限认证、日志、统计的场景,可以使用该拦截器。

第一步,继承HandlerInterceptorAdapter类定义拦截器:

public  class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestUrl = request.getRequestURI();
        if (checkAuth(requestUrl)) {
            return  true;
        }

        return  false;
    }

    private boolean checkAuth(String requestUrl) {
        System.out.println("===权限校验===");
        return  true;
    }
}

第二步,将该拦截器注册到spring容器:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 不需要登录拦截的url
     */
    final String[] notLoginInterceptPaths = {
            "/home",
            "/demo/**"
    };

    @Bean
    public HandlerInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(notLoginInterceptPaths);
    }
}

原理:在请求接口时spring mvc通过该拦截器,能够自动拦截该接口,并且校验权限。

该拦截器其实相对来说,比较简单,可以在DispatcherServlet类的doDispatch方法中看到调用过程:
在这里插入图片描述

四、全局异常处理

通过ControllerAdvice拦截所有的请求,拦截到对应的异常后,封装成统一的实体,友好的返回,可以根据拦截的异常类型分类处理,也可以捕捉更大的异常统一处理

@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalControllerExceptionHandler {

    @ExceptionHandler({HttpMessageNotReadableException.class, MissingServletRequestParameterException.class,
            TypeMismatchException.class})
    public ResponseEntity<ErrorResponse> handleException(Exception e) {
        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.BAD_REQUEST.value(),
                GlobalErrorResponseConstants.REQUEST_ERROR_CODE + "", GlobalErrorResponseConstants.REQUEST_ERROR_MESSAGE,
                e.getMessage());
        log.warn("[BadRequestWarn]: " + combineRequestMetaAndResponse(errorResponse), e);
        return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResponse> handleException(MethodArgumentNotValidException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return convertToResponseEntity(allErrors);
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResponse> handleException(BindException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return convertToResponseEntity(allErrors);
    }

    private ResponseEntity<ErrorResponse> convertToResponseEntity(List<ObjectError> allErrors) {
        String developerMessage = "";
        for (ObjectError error : allErrors) {
            developerMessage += error.getDefaultMessage() + "; ";
        }
        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.UNPROCESSABLE_ENTITY.value(),
                GlobalErrorResponseConstants.REQUEST_ERROR_CODE + "", GlobalErrorResponseConstants.REQUEST_ERROR_MESSAGE,
                developerMessage);
        log.warn("[ClientWarn]: " + combineRequestMetaAndResponse(errorResponse));
        return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.UNPROCESSABLE_ENTITY);
    }

    private String combineRequestMetaAndResponse(ErrorResponse errorResponse) {
        return this.combineRequestMetaAndResponse(errorResponse, null);
    }

    private String combineRequestMetaAndResponse(ErrorResponse errorResponse, String url) {
        Map<String, Object> requestMeta = Maps.newHashMap();
        requestMeta.put("requestApi", StatisticsThreadLocal.getApiName());
        requestMeta.put("requestId", ParameterThreadLocal.getRequestId());
        requestMeta.put("httpOutUrl", url);
        requestMeta.putAll(errorResponse.toMap());
        return JSON.toJSONString(requestMeta);
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResponse> handleException(BaseException e) {
        ErrorResponse errorResponse = new ErrorResponse(e.getHttpStatus(), e.getCode(), e.getMessage(),
                e.getDeveloperMessage(), e.getErrorLevel());
        if (e instanceof ServerException) {
            log.error("[ServerError]: " + combineRequestMetaAndResponse(errorResponse), e);
        } else {
            log.warn("[ClientWarn]: " + combineRequestMetaAndResponse(errorResponse), e);
        }
        return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.valueOf(e.getHttpStatus()));
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResponse> handleException(HttpClientException e) {

        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(),
                GlobalErrorResponseConstants.INTERNAL_CALL_ERROR_CODE + "",
                GlobalErrorResponseConstants.INTERNAL_CALL_ERROR_MESSAGE, e.getMessage());

        if (e.getErrorResponse() != null) {
            errorResponse = e.getErrorResponse();
        }

        Integer httpStatusCode = e.getHttpStatusCode();
        if (httpStatusCode != null) {
            httpStatus = HttpStatus.valueOf(httpStatusCode);
            log.warn("[HttpWarn]: " + combineRequestMetaAndResponse(errorResponse, e.getUrl()));
        } else {
            log.error("[HttpError]: " + combineRequestMetaAndResponse(errorResponse, e.getUrl()), e);
        }
        return new ResponseEntity<ErrorResponse>(errorResponse, httpStatus);
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResponse> handleGeneralException(Exception e) {
        ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(),
                GlobalErrorResponseConstants.COMMON_SERVER_ERROR_CODE + "",
                GlobalErrorResponseConstants.COMMON_SERVER_ERROR_MESSAGE, e.getMessage());
        log.error("[UncaughtError]: " + combineRequestMetaAndResponse(errorResponse), e);
        return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

全局异常响应实体类

@Data
public class ErrorResponse {

    private int status;
    private String code;
    private String message;
    private String developerMessage;
    private String errorLevel;

    public ErrorResponse() {
    }

    public ErrorResponse(int status, String code, String message, String developerMessage) {

        super();
        this.status = status;
        this.code = code;
        this.message = message;
        this.developerMessage = developerMessage;
    }

    public ErrorResponse(int status, String code, String message, String developerMessage, String errorLevel) {

        super();
        this.status = status;
        this.code = code;
        this.message = message;
        this.developerMessage = developerMessage;
        this.errorLevel = errorLevel;
    }


    public Map<String, Object> toMap() {

        Map<String, Object> map = Maps.newHashMap();
        map.put("status", status);
        map.put("code", code);
        map.put("message", message);
        map.put("developerMessage", developerMessage);
        map.put("errorLevel", errorLevel);
        return map;
    }

}

全局异常响应码

public class GlobalErrorResponseConstants {

    public static final int COMMON_SERVER_ERROR_CODE = 1001;
    public static final String COMMON_SERVER_ERROR_MESSAGE = "系统忙不过来啦,稍等一下";

    public static final int COMMON_CLIENT_ERROR_CODE = 1005;
    public static final String COMMON_CLIENT_ERROR_MESSAGE = "您的操作有误,重新试试吧";

    public static final int REQUEST_ERROR_CODE = 1002;
    public static final String REQUEST_ERROR_MESSAGE = "输入信息有误,重新试试吧";

    public static final int COMMON_BIZ_ERROR_CODE = 1003;

    public static final int INTERNAL_CALL_ERROR_CODE = 1004;
    public static final String INTERNAL_CALL_ERROR_MESSAGE = "系统忙不过来啦,稍等一下";

    public static final int SESSION_TIMEOUT_ERROR_CODE = 1006;
    public static final String SESSION_TIMEOUT_ERROR_MESSAGE = "发呆的时间太长,请先登录哦";

}

五、springboot异步处理

第一步,springboot项目启动类上加@EnableAsync注解。

@EnableAsync
@SpringBootApplication
public  class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.SERVLET).run(args);
    }
}

第二步,在需要使用异步的方法上加上@Async注解:

@Service
public  class PersonService {

    @Async
    public String get() {
        System.out.println("===add==");
        return  "data";
    }
}

然后在使用的地方调用一下:personService.get();就拥有了异步功能,是不是很神奇。

默认情况下,spring会为我们的异步方法创建一个线程去执行,如果该方法被调用次数非常多的话,需要创建大量的线程,会导致资源浪费。
这时,我们可以定义一个线程池,异步方法将会被自动提交到线程池中执行。

线程池配置类,读取配置文件中的配置,初始化线程池,这里先给上默认配置

@ConfigurationProperties(prefix = AsyncProperties.PROPERTY_PREFIX)
@Data
public class AsyncProperties {

    public final static String PROPERTY_PREFIX = "async.executor";

    private int corePoolSize = 5;
    private int maxPoolSize = 10;
    private int keepAliveSeconds = 60;
    private int queueCapacity = 20;
}

自定义线程池,此处加上@EnableAsync,就不需要在启动类上添加了

@Configuration
@EnableAsync
@EnableConfigurationProperties({AsyncProperties.class})
@ConditionalOnProperty(prefix = AsyncProperties.PROPERTY_PREFIX, value = "enabled")
public class AsyncConfig{
    @Autowired
    private AsyncProperties asyncProperties;

    @Bean(name = "taskExecutor")
    public Executor frameworkAsync() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(asyncProperties.getCorePoolSize());
        executor.setMaxPoolSize(asyncProperties.getMaxPoolSize());
        executor.setKeepAliveSeconds(asyncProperties.getKeepAliveSeconds());
        executor.setQueueCapacity(asyncProperties.getQueueCapacity());
        /* CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行 */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

根据返回值不同,处理情况也不太一样,具体分为如下情况:
在这里插入图片描述

六、自定义Enable开关

不知道你有没有用过Enable开头的注解,比如:EnableAsync、EnableCaching、EnableAspectJAutoProxy等,这类注解就像开关一样,只要在@Configuration定义的配置类上加上这类注解,就能开启相关的功能。

是不是很酷?

让我们一起实现一个自己的开关:

第一步,定义一个LogFilter:

public  class LogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("记录请求日志");
        chain.doFilter(request, response);
        System.out.println("记录响应日志");
    }

    @Override
    public void destroy() {
        
    }
}

第二步,注册LogFilter:

@Configuration
public  class LogFilterWebConfig {

    @Bean
    public LogFilter timeFilter() {
        return  new LogFilter();
    }
}

第三步,定义开关@EnableLog注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(LogFilterWebConfig.class)
public @interface EnableLog {

}

第四步,只需在springboot启动类加上@EnableLog注解即可开启LogFilter记录请求和响应日志的功能。

七、自定义Scope

我们都知道spring默认支持的Scope只有两种:

  • singleton 单例,每次从spring容器中获取到的bean都是同一个对象。
  • prototype多例,每次从spring容器中获取到的bean都是不同的对象。

spring web又对Scope进行了扩展,增加了:

  • RequestScope 同一次请求从spring容器中获取到的bean都是同一个对象。
  • SessionScope 同一个会话从spring容器中获取到的bean都是同一个对象。

即便如此,有些场景还是无法满足我们的要求。

比如,我们想在同一个线程中从spring容器获取到的bean都是同一个对象,该怎么办?

这就需要自定义Scope了。

第一步实现Scope接口:

public  class ThreadLocalScope implements Scope {

    private  static  final ThreadLocal THREAD_LOCAL_SCOPE = new ThreadLocal();

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Object value = THREAD_LOCAL_SCOPE.get();
        if (value != null) {
            return value;
        }

        Object object = objectFactory.getObject();
        THREAD_LOCAL_SCOPE.set(object);
        return object;
    }

    @Override
    public Object remove(String name) {
        THREAD_LOCAL_SCOPE.remove();
        return  null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {

    }

    @Override
    public Object resolveContextualObject(String key) {
        return  null;
    }

    @Override
    public String getConversationId() {
        return  null;
    }
}

第二步将新定义的Scope注入到spring容器中:

@Component
public  class ThreadLocalBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.registerScope("threadLocalScope", new ThreadLocalScope());
    }
}

第三步使用新定义的Scope:

@Scope("threadLocalScope")
@Service
public  class CService {

    public void add() {
    }
}

八、本地缓存spring cache

spring cache架构图:
在这里插入图片描述
它目前支持多种缓存:
在这里插入图片描述
我们在这里以caffeine为例,它是spring官方推荐的。

第一步,引入caffeine的相关jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>2.6.0</version>
</dependency>

第二步,配置CacheManager,开启EnableCaching

@Configuration
@EnableCaching
public  class CacheConfig {
    @Bean
    public CacheManager cacheManager(){
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        //Caffeine配置
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                //最后一次写入后经过固定时间过期
                .expireAfterWrite(10, TimeUnit.SECONDS)
                //缓存的最大条数
                .maximumSize(1000);
        cacheManager.setCaffeine(caffeine);
        return cacheManager;
    }
}

第三步,使用Cacheable注解获取数据

@Service
public  class CategoryService {
   
   //category是缓存名称,#type是具体的key,可支持el表达式
   @Cacheable(value = "category", key = "#type")
   public CategoryModel getCategory(Integer type) {
       return getCategoryByType(type);
   }

   private CategoryModel getCategoryByType(Integer type) {
       System.out.println("根据不同的type:" + type + "获取不同的分类数据");
       CategoryModel categoryModel = new CategoryModel();
       categoryModel.setId(1L);
       categoryModel.setParentId(0L);
       categoryModel.setName("电器");
       categoryModel.setLevel(3);
       return categoryModel;
   }
}

调用categoryService.getCategory()方法时,先从caffine缓存中获取数据,如果能够获取到数据则直接返回该数据,不会进入方法体。如果不能获取到数据,则直接方法体中的代码获取到数据,然后放到caffine缓存中。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值