Springboot @Autowired无法注入怎么办

发布时间:2021-06-28 15:06:25 作者:小新
来源:亿速云 阅读:153

这篇文章给大家分享的是有关Springboot @Autowired无法注入怎么办的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

特别提醒:一定要注意文件结构

  WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入。

1.  添加工具类获取在 Spring 中托管的 Bean

  (1)工具类

package com.common;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * @program: IPC_1P
 * @description: 获取在spring中托管的bean
 * @author: johnny
 * @create: 2018-08-03 16:24
 **/
public class SpringContextUtil {
  private static ApplicationContext applicationContext; // Spring应用上下文
  // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
  public static void setApplicationContext(ApplicationContext applicationContext)
      throws BeansException {
    SpringContextUtil.applicationContext = applicationContext;
  }
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }
  public static Object getBean(String name, Class requiredType)
      throws BeansException {
    return applicationContext.getBean(name, requiredType);
  }
  public static boolean containsBean(String name) {
    return applicationContext.containsBean(name);
  }
  public static boolean isSingleton(String name)
      throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(name);
  }
  public static Class getType(String name)
      throws NoSuchBeanDefinitionException {
    return applicationContext.getType(name);
  }
  public static String[] getAliases(String name)
      throws NoSuchBeanDefinitionException {
    return applicationContext.getAliases(name);
  }
}

  (2)使用

    1)程序启动时,实例化 SpringContextUtil

@SpringBootApplication
public class WebappApplication {
  private static ApplicationContext applicationContext;
  public static void main(String[] args) {
    applicationContext = SpringApplication.run(WebappApplication.class, args);
    //
    SpringContextUtil springContextUtil = new SpringContextUtil();
    springContextUtil.setApplicationContext(applicationContext);
    System.out.println("服务器启动测试!");
}

    2)在使用 @Service 的方法中,通过@Autowired 注入,使用SpringcontexUtil 获取Bean上下文

@Autowired
  SenderService senderService;
public class Package_State {
  @Autowired
  SenderService senderService;
  @Component
  private Package_State() {
    senderService = (SenderService)SpringContextUtil.getBean("senderService");
  }
}

感谢各位的阅读!关于“Springboot @Autowired无法注入怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 浅谈Springboot中Autowried及Resouce
  2. SpringBoot多线程处理任务无法@Autowired注入bean怎么办

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot @autowired

上一篇:docker-compose的安装方法

下一篇:Android中SDK方法如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》