在使用spring的过程中,我们有没有发现它的扩展能力很强呢? 由于这个优势的存在,使得spring具有很强的包容性,所以很多第三方应用或者框架可以很容易的投入到spring的怀抱中。今天我们主要来学习Spring中很常用的11个扩展点,你用过几个呢?
如果接口中接收参数的实体对象中,有一个字段类型为Date,但实际传递的参数是字符串类型:2022-12-15 10:20:15,该如何处理?
Spring提供了一个扩展点,类型转换器Type Converter,具体分为3类:
还是不明白的话,我们举个例子吧。
- @Data
- public class User {
- private Long id;
- private String name;
- private Date registerDate;
- }
-
- public class DateConverter implements Converter<String, Date> {
- private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- @Override
- public Date convert(String source) {
- if (source != null && !"".equals(source)) {
- try {
- simpleDateFormat.parse(source);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
-
- @Configuration
- public class WebConfig extends WebMvcConfigurerAdapter {
- @Override
- public void addFormatters(FormatterRegistry registry) {
- registry.addConverter(new DateConverter());
- }
- }
-
- @RequestMapping("/user")
- @RestController
- public class UserController {
- @RequestMapping("/save")
- public String save(@RequestBody User user) {
- return "success";
- }
- }
-
请求接口时,前端传入的日期字符串,会自动转换成Date类型。
在我们日常开发中,经常需要从Spring容器中获取bean,但是你知道如何获取Spring容器对象吗?
- @Service
- public class PersonService 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");
- }
- }
-
实现BeanFactoryAware接口,然后重写setBeanFactory方法,可以从方法中获取spring容器对象。
- @Service
- public class PersonService2 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");
- }
- }
-
实现ApplicationContextAware接口,然后重写setApplicationContext方法,也可以通过该方法获取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");
- }
- }
-
以往我们在开发界面的时候,如果出现异常,要给用户更友好的提示,例如:
- @RequestMapping("/test")
- @RestController
- public class TestController {
-
- @GetMapping("/add")
- public String add() {
- int a = 10 / 0;
- return "su";
- }
- }
-
如果不对请求添加接口结果做任何处理,会直接报错:
用户可以直接看到错误信息吗?
这种交互给用户带来的体验非常差。 为了解决这个问题,我们通常在接口中捕获异常:
- @GetMapping("/add")
- public String add() {
- String result = "success";
- try {
- int a = 10 / 0;
- } catch (Exception e) {
- result = "error";
- }
- return result;
- }
-
界面修改后,出现异常时会提示:“数据异常”,更加人性化。
看起来不错,但是有一个问题。
如果只是一个接口还好,但是如果项目中有成百上千个接口,还得加异常捕获代码吗?
答案是否定的,这就是全局异常处理派上用场的地方:RestControllerAdvice。
- @RestControllerAdvice
- public class GlobalExceptionHandler {
-
- @ExceptionHandler(Exception.class)
- public String handleException(Exception e) {
- if (e instanceof ArithmeticException) {
- return "data error";
- }
- if (e instanceof Exception) {
- return "service error";
- }
- retur null;
- }
- }
-
方法中处理异常只需要handleException,在业务接口中就可以安心使用,不再需要捕获异常(统一有人处理)。
Spring MVC拦截器,它可以获得HttpServletRequest和HttpServletResponse等web对象实例。
Spring MVC拦截器的顶层接口是HandlerInterceptor,它包含三个方法:
为了方便,我们一般继承HandlerInterceptorAdapter,它实现了HandlerInterceptor。
如果有授权鉴权、日志、统计等场景,可以使用该拦截器,我们来演示下吧。
- 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) {
- return true;
- }
- }
-
- @Configuration
- public class WebAuthConfig extends WebMvcConfigurerAdapter {
-
- @Bean
- public AuthInterceptor getAuthInterceptor() {
- return new AuthInterceptor();
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new AuthInterceptor());
- }
- }
-
有时我们需要在某个配置类中引入其他的类,引入的类也加入到Spring容器中。 这时候可以使用注解@Import来完成这个功能。
如果你查看它的源代码,你会发现导入的类支持三种不同的类型。
但是我觉得最好把普通类的配置类和@Configuration注解分开解释,所以列出了四种不同的类型:
这种引入方式是最简单的,引入的类会被实例化为一个bean对象。
- public class A {
- }
-
- @Import(A.class)
- @Configuration
- public class TestConfiguration {
-
- }
-
通过@Import注解引入类A,spring可以自动实例化A对象,然后在需要使用的地方通过注解@Autowired注入:
- @Autowired
- private A a;
-
这种引入方式是最复杂的,因为@Configuration支持还支持多种组合注解,比如:
- public class A {
- }
-
- public class B {
- }
-
- @Import(B.class)
- @Configuration
- public class AConfiguration {
-
- @Bean
- public A a() {
- return new A();
- }
- }
-
- @Import(AConfiguration.class)
- @Configuration
- public class TestConfiguration {
- }
-
@Configuration注解的配置类通过@Import注解导入,配置类@Import、@ImportResource相关注解引入的类会一次性全部递归引入@PropertySource所在的属性。
该导入方法需要实现ImportSelector接口
- public class AImportSelector implements ImportSelector {
-
- private static final String CLASS_NAME = "com.sue.cache.service.test13.A";
-
- public String[] selectImports(AnnotationMetadata importingClassMetadata) {
- return new String[]{CLASS_NAME};
- }
- }
-
- @Import(AImportSelector.class)
- @Configuration
- public class TestConfiguration {
- }
-
这种方法的好处是selectImports方法返回的是一个数组,也就是说可以同时引入多个类,非常方便。
该导入方法需要实现ImportBeanDefinitionRegistrar接口:
- public class AImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
- @Override
- public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
- RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(A.class);
- registry.registerBeanDefinition("a", rootBeanDefinition);
- }
- }
-
- @Import(AImportBeanDefinitionRegistrar.class)
- @Configuration
- public class TestConfiguration {
- }
-
这种方法是最灵活的。 容器注册对象可以在registerBeanDefinitions方法中获取,可以手动创建BeanDefinition注册到BeanDefinitionRegistry种。
有时候我们需要在项目启动的时候自定义一些额外的功能,比如加载一些系统参数,完成初始化,预热本地缓存等。 我们应该做什么?
好消息是 SpringBoot 提供了:
这两个接口帮助我们实现了上面的需求。
它们的用法很简单,以ApplicationRunner接口为例:
- @Component
- public class TestRunner implements ApplicationRunner {
-
- @Autowired
- private LoadDataService loadDataService;
-
- public void run(ApplicationArguments args) throws Exception {
- loadDataService.load();
- }
- }
-
实现ApplicationRunner接口,重写run方法,在该方法中实现您的自定义需求。
如果项目中有多个类实现了ApplicationRunner接口,如何指定它们的执行顺序?
答案是使用@Order(n)注解,n的值越小越早执行。 当然,顺序也可以通过@Priority注解来指定。
在实例化Bean对象之前,Spring IOC需要读取Bean的相关属性,保存在BeanDefinition对象中,然后通过BeanDefinition对象实例化Bean对象。
如果要修改BeanDefinition对象中的属性怎么办?
答案:我们可以实现BeanFactoryPostProcessor接口。
- @Component
- public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
-
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
- DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
- BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
- beanDefinitionBuilder.addPropertyValue("id", 123);
- beanDefinitionBuilder.addPropertyValue("name", "Tom");
- defaultListableBeanFactory.registerBeanDefinition("user", beanDefinitionBuilder.getBeanDefinition());
- }
- }
-
在postProcessBeanFactory方法中,可以获取BeanDefinition的相关对象,修改对象的属性。
有时,您想在 bean 初始化前后实现一些您自己的逻辑。
这时候就可以实现:BeanPostProcessor接口。
该接口目前有两个方法:
- @Component
- public class MyBeanPostProcessor implements BeanPostProcessor {
-
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- if (bean instanceof User) {
- ((User) bean).setUserName("Tom");
- }
- return bean;
- }
- }
-
我们经常使用的@Autowired、@Value、@Resource、@PostConstruct等注解都是通过AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor来实现的。
目前在Spring中初始化bean的方式有很多种:
- @Service
- public class AService {
- @PostConstruct
- public void init() {
- System.out.println("===init===");
- }
- }
-
为需要初始化的方法添加注解@PostConstruct,使其在Bean初始化时执行。
- @Service
- public class BService implements InitializingBean {
-
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("===init===");
- }
- }
-
实现InitializingBean接口,重写afterPropertiesSet方法,在该方法中可以完成初始化功能。
有时候,我们需要在关闭spring容器之前做一些额外的工作,比如关闭资源文件。
此时你可以实现DisposableBean接口并重写它的destroy方法。
- @Service
- public class DService implements InitializingBean, DisposableBean {
-
- @Override
- public void destroy() throws Exception {
- System.out.println("DisposableBean destroy");
- }
-
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("InitializingBean afterPropertiesSet");
- }
- }
-
这样,在spring容器销毁之前,会调用destroy方法做一些额外的工作。
通常我们会同时实现InitializingBean和DisposableBean接口,重写初始化方法和销毁方法。
我们都知道spring core默认只支持两种Scope:
Spring Web 再次扩展了 Scope,添加
尽管如此,有些场景还是不符合我们的要求。
比如我们在同一个线程中要从spring容器中获取的bean都是同一个对象,怎么办?
答案:这需要一个自定义范围。
- 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;
- }
- }
-
- @Component
- public class ThreadLocalBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
- beanFactory.registerScope("threadLocalScope", new ThreadLocalScope());
- }
- }
-
- @Scope("threadLocalScope")
- @Service
- public class CService {
- public void add() {
- }
- }
-
本文总结了Spring中很常用的11个扩展点,可以在Bean创建、初始化到销毁各个阶段注入自己想要的逻辑,也有Spring MVC相关的拦截器等扩展点,希望对大家有帮助。