解决Springboot @Autowired 无法注入问题
作者:不靠谱斯基 时间:2022-04-14 21:19:01
特别提醒:一定要注意文件结构
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 无法注入问题网站的支持!
来源:https://www.cnblogs.com/sylarken/archive/2018/08/07/9435076.html
标签:spring,boot,注入
0
投稿
猜你喜欢
C# 操作符之二 算数操作符
2023-06-19 20:21:13
Java类的初始化顺序知识点总结
2021-09-23 23:19:26
SpringBoot 自定义starter yaml提示失效问题及解决方法
2022-08-03 14:58:42
mybatis @Alias注解在类上的使用方式(推荐)
2023-11-20 00:30:03
C语言时间函数之strftime()详解
2023-06-26 02:42:32
java制作带界面的聊天工具
2022-10-29 03:40:01
Android提醒微技巧你真的了解Dialog、Toast和Snackbar吗
2023-03-08 14:15:44
SpringMVC接收多个对象的4种方法
2023-11-23 06:24:18
C++实现约瑟夫环的循环单链表
2022-11-12 19:34:29
解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题
2022-06-25 06:48:17
Spring Security OAuth过期的解决方法
2023-05-26 22:30:01
解读Spring定义Bean的两种方式:<bean>和@Bean
2023-01-25 23:37:51
关于SpringBoot静态资源路径管理问题
2022-12-04 03:51:29
Android实现闪屏页效果
2022-01-17 01:21:47
MyBatis动态SQL特性详解
2022-10-03 03:21:50
Android中去掉标题栏的几种方法(三种)
2023-04-01 10:57:37
Spring Boot修改内置Tomcat默认端口号的示例
2023-03-29 21:11:40
C语言中传值与传指针的介绍与区别
2023-08-01 15:26:51
Android TextView实现跑马灯效果的方法
2023-07-30 20:44:12
C#判断某个软件是否已安装实现代码分享
2022-07-15 16:34:06