[解决]spring boot 中Interceptor里 自动注入的对象为bull

今天在实现login拦截器后,运行时候突然发现userMapper.fingbyId()空指针。然后自己测试了一下


    @Test
    public void find() {
        if (userMapper==null){
            System.out.println("userMapper为空");
        }
        userMapper.find();
    }

结果居然发现自动注入mapper对象是空的
在这里插入图片描述
首先排除了注解错误,数据源错误的问题,后来突然想到spring容器在管理Bean自动注入时,如果是自己new创建的对象就不会自动注入。而我在使用WebMvcConfigurer类添加拦截器时候是通过new创建的

InterceptorRegistration interceptorRegistry=registry.addInterceptor(new LoginInterceptor());

这也导致了 LoginInterceptor类中对象没有自动注入
这里有两种方式解决。

方案一
        if (userMapper==null){
            BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
            userMapper = (UserMapper) factory.getBean("userMapper");
       }

spring容器不自动注入,我们就进行手动注入,获取到BeanFactory后通过getBean找到对象注入

方案二

不使用直接使用new 创建 LoginInterceptor对象,将getloginInterceptor()加上@Bean注解交给spring容器管理就可以注入对象了

    @Bean
    LoginInterceptor getloginInterceptor(){
        return new LoginInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistry=registry.addInterceptor(getloginInterceptor());
    }

本文参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot provides a way to intercept method calls using Method Interceptor. Method Interceptor allows you to intercept a method call before and after the method is called. This is useful when you want to add some additional behavior to a method, such as logging or security checks. To create a method interceptor in Spring Boot, you need to create a class that implements the `MethodInterceptor` interface from the `org.aopalliance.intercept` package. This interface has two methods: 1. `Object invoke(MethodInvocation invocation) throws Throwable`: This method is called before and after the method is executed. The `MethodInvocation` object contains information about the method call and allows you to manipulate the method parameters and return value. 2. `MethodInterceptor getInterceptor()`: This method returns the actual interceptor that will be used to intercept the method call. Here's an example of how to create a method interceptor in Spring Boot: ```java import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class LoggingInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Before method call: " + invocation.getMethod().getName()); Object result = invocation.proceed(); System.out.println("After method call: " + invocation.getMethod().getName()); return result; } } ``` In this example, the `LoggingInterceptor` class implements the `MethodInterceptor` interface and overrides the `invoke` method to log the method name before and after the actual method call. To use this interceptor, you need to configure it in your Spring Boot application context: ```java import org.springframework.aop.framework.ProxyFactoryBean; @Configuration public class AppConfig { @Bean public LoggingInterceptor loggingInterceptor() { return new LoggingInterceptor(); } @Bean public MyService myService() { ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean(); proxyFactoryBean.setTarget(new MyServiceImpl()); proxyFactoryBean.setInterceptorNames("loggingInterceptor"); return (MyService) proxyFactoryBean.getObject(); } } ``` In this example, the `loggingInterceptor` bean is created and configured to intercept method calls. The `MyService` bean is also created and configured to use the `loggingInterceptor` for method interception. When a method is called on the `MyService` bean, the `LoggingInterceptor` will intercept the method call and log the method name before and after the actual method call.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值