springboot 对于@Autowired 注入为null 的介绍与解决方式

1、描述

有时候我们在某个类用@Autowired 进行注入时,会发现注入参数为null,这时候会有疑惑。

可能存在的原因:

(1)该类没有托管给spring 管理,一般在类的上面添加@Component

(2)你的这个类有被new出来的实例的,new 过的对象不会交给Spring容器管理 所以里面的 service或者dao注入不进来。一般是指引用某些框架,你是继承某个接口,但是这些框架默认new过这个方法,比如MVC拦截的HandlerInterceptor类。

2、解决方式

对于(1)其实就加入@Component 就可以了。接下来讲怎么解决第(2)种情况:

有时候你确实需要在这个new 的类去注入某些类,但是用@Autowired 又注入为null,这时候我们需要手动去弄Spring容器中的Bean实现ApplicationContextAware接口。

(1)比如我想在某个类实现RedisUtils 类的注入,但是用@autowired 会报null

(2)这时候我们就要手动实现这个功能,写个BeanUtils 类 实现ApplicationContextAware接口



import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class BeanUtils implements ApplicationContextAware  {
    protected static ApplicationContext applicationContext ;

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        if (applicationContext == null) {
            applicationContext = arg0;
        }

    }
    public static Object getBean(String name) {
        //name表示其他要注入的注解name名
        return applicationContext.getBean(name);
    }

    /**
     * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}

注意,该类必须用@Component 注解

(3)使用时用以下方式

    /**
     * 由于不能用@Autowired进行注入,则使用这种手动注入方式
     */
    private RedisUtils redisUtils = BeanUtils.getBean(RedisUtils.class);

这样就要正常引用redisUtils ,这样手动出来的跟@Autowire 一样的。

  • 25
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
在Spring中,@Autowired注解用于自动装配依赖项。对于方法级别的@Autowired,可以使用在方法上,以实现自动装配方法参数。 当使用@Autowired在方法上时,Spring会尝试将匹配的bean自动注入到方法参数中。它使用与字段注入相同的规则来解析依赖关系。 例如,考虑以下示例代码: ```java @Service public class MyService { private MyRepository myRepository; @Autowired public void setMyRepository(MyRepository myRepository) { this.myRepository = myRepository; } // 其他方法 } @Repository public class MyRepository { // 实现 } ``` 在上面的例子中,@Autowired注解应用于setMyRepository方法上。当Spring实例化MyService类时,它会查找匹配类型的bean(在这种情况下是MyRepository)并自动将其注入到setMyRepository方法的参数中。 这样,MyService就可以在其他方法中使用myRepository实例,而无需手动实例化或注入它。 总结起来,@Autowired在方法级别上的使用,可以实现方法参数的自动装配,简化了代码并提高了开发效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【保姆级】超超超简单的自定义注解实现@Autowired同款功能](https://blog.csdn.net/aqin1012/article/details/128938759)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [SpringIoCAnnotation:Spring IoC @Annotation-有和没有@Autowired](https://download.csdn.net/download/weixin_42120563/15284697)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值