Spring实战-遇到的问题记录系列--@Autowired注入为空

Springboot中new出来的实例中含有@Autowired注入时的Spring Bean为NULL

Springboot中new出来的实例中含有@Autowired注入时的Spring Bean为NULL
https://blog.csdn.net/Mr_Runner/article/details/83684088

原因分析

new出来的实例中含有@Autowired注入时,注入的Bean为null;

@Autowired注入时是将类交给Springboot管理,而new出来的实例脱离了Springboot的管理,两个东西不在一个管理者管理下,所以没法联系在一起,@Autowired注入就会为null。
解决方法:
不要用new的方式实例化,也采用注解的方式,在需要new的实例类上加@Component注解,通过注入的方式使用实例化类;

代码实现参考

springboot中通过main方法调用service,daohttps://blog.csdn.net/qq_25925973/article/details/90751870

大多数情况下,我们使用springboot是创建一个web项目,然后通过接口访问,但是也有特殊情况,比如线上跑着的web项目,有一些特殊的数据,需要经过计算导入到数据库,这个时候,我们可能需要原来的web项目中的一些service,dao才辅助操作,但是又不能在服务端新开接口。我们通过springboot的main方法执行这些操作。

此时,service和到需要通过上下文获得。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
 
/**
 * 普通类调用Spring bean对象:
 * 注意:此类需要放到App.java同包或者子包下才能被扫描,否则失效。
 */
 
@Component
public class SpringUtil implements ApplicationContextAware{
 
    private static ApplicationContext applicationContext = null;
     @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       if(SpringUtil.applicationContext == null){
           SpringUtil.applicationContext  = applicationContext;
       }
      
    }
     //获取applicationContext
    public static ApplicationContext getApplicationContext() {
       return applicationContext;
    }
     //通过name获取 Bean.
    publicstatic Object getBean(String name){
       return getApplicationContext().getBean(name);
     }
     //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
       return getApplicationContext().getBean(clazz);
    }
     //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
       return getApplicationContext().getBean(name, clazz);
    }
}

在主方法中:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class RabbitmqproviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitmqproviderApplication.class, args);

        ApplicationContext context = SpringUtil.getApplicationContext();
        SendMessageService  service = context.getBean(SendMessageService.class);
        service.sendDirectMessage();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC框架中,我们通常使用@Autowired注解来注入Service或Mapper接口。在controller层中注入service接口,在service层中注入其他的service接口或mapper接口是可以的。然而,在我们自定义的非controller或service类中直接使用@Autowired注解进行注入是不可能的,会导致空指针异常。 解决这个问题的方法是,我们可以使用下面的方式来实现注入。首先,在我们自定义的类中声明一个@Autowired注解的成员变量,然后通过构造方法或setter方法来对该成员变量进行赋值。这样,在使用该类时,Spring会自动将依赖的实例注入进来,避免了空指针异常的发生。具体实现步骤和相关使用技巧可以参考Spring实战之@Autowire注解的文章。 举个例子,假设我们有一个非controller或service类叫做UserInfoService,在该类中需要注入一个IUserInfoService接口的实例。我们可以在UserInfoService类中声明如下成员变量并加上@Autowired注解: @Autowired private IUserInfoService userInfoService; 然后,在UserInfoService类的构造方法或setter方法中对该成员变量进行赋值: public UserInfoService(IUserInfoService userInfoService) { this.userInfoService = userInfoService; } 或者 @Autowired public void setUserInfoService(IUserInfoService userInfoService) { this.userInfoService = userInfoService; } 这样,在使用UserInfoService类时,IUserInfoService接口的实例就会被自动注入进来,避免了空指针异常的发生。如果你仍然遇到service层加了@Autowire注解后报空指针异常的问题,请检查是否正确声明了@Autowired注解的成员变量并是否正确进行了赋值。如果仍然无法解决问题,可以参考引用中的解决办法,可能会帮助你解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值