Spring中让人眼前一亮的11个小技巧

前言

我们一说到spring,可能第一个想到的是 IOC(控制反转) 和 AOP(面向切面编程)。

没错,它们是spring的基石,得益于它们的优秀设计,使得spring能够从众多优秀框架中脱颖而出。

除此之外,我们在使用spring的过程中,有没有发现它的扩展能力非常强。由于这个优势的存在,让spring拥有强大的包容能力,让很多第三方应用能够轻松投入spring的怀抱。比如:rocketmq、mybatis、redis等。

今天跟大家一起聊聊,在Spring中最常用的11个扩展点。

1.自定义拦截器

spring mvc拦截器根spring拦截器相比,它里面能够获取HttpServletRequestHttpServletResponse等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 WebAuthConfig extends WebMvcConfigurerAdapter {
   
 
    @Bean
    public AuthInterceptor getAuthInterceptor() {
   
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
   
        registry.addInterceptor(new AuthInterceptor());
    }
}

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

2.获取Spring容器对象

在我们日常开发中,经常需要从Spring容器中获取Bean,但你知道如何获取Spring容器对象吗?

2.1 BeanFactoryAware接口

@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容器对象。

2.2 ApplicationContextAware接口

@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容器对象。

2.3 ApplicationListener接口

@Service
public class PersonService3 implements ApplicationListener<ContextRefreshedEvent> {
   
    private 
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏三说技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值