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");
}
}
Java 初学,原理理解不很透彻,只针对遇到的问题进行记录,随着学习的深入随时修改
Springboot @Autowired 无法注入问题的更多相关文章
- SpringBoot @Autowired中注入静态方法或者静态变量
注:用static去定义一个注入的方法或者配置文件值变量,编译时不会有任何异常,运行时会报空指针. Spring官方不推荐此种方法. 原理: https://www.cnblogs.com/chenf ...
- 正确理解springboot的常用注入方式
springboot的属性注入 以注入dataSource为例1.springboot默认读取的文件是放在resources目录下的名为application.properties或applicati ...
- 解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的s ...
- 解决非controller使用@Autowired注解注入为null问题
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...
- 深入理解springboot的自动注入
一.开篇 在平时的开发过程中用的最多的莫属springboot了,都知道springboot中有自动注入的功能,在面试过程中也会问到自动注入,你知道自动注入是怎么回事吗,springboot是如何 ...
- 解决SpringBoot的@Autowired无法注入问题
问题:@Autowired无法自动注入 思路:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!"Application类"是指 ...
- spring springboot websocket 不能注入( @Autowired ) service bean 报 null 错误
spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入). ...
- 基于SpringBoot的多模块项目引入其他模块时@Autowired无法注入其他模块stereotype注解类对象的问题解决
类似问题: 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案 排查原因总结如下 ...
- SpringBoot中普通类无法通过@Autowired自动注入Service、dao等bean解决方法
无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com ...
随机推荐
- JS高级学习历程-17
[正则案例] 1 匹配手机号码
- background-attachment:fixed不兼容性
ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了,代码如下: ps:想在哪个标签加背景,可以在它class后:before ...
- ES6入门教程---数值扩展和数组扩展
1.数值扩展 var num = 0b11; console.log(num);3 var num = 0o11;console.log(num);9 var num = 1.234; console ...
- Django中间件的执行流程
Django中间件的执行流程. 请求到达中间件之后,先按照正序执行每个注册中间件的process_reques方法,process_request方法返回的值是None,就依次执行, 如果返回的值是H ...
- C.One Piece
链接:https://ac.nowcoder.com/acm/contest/908/C 题意: Luffy once saw a particularly delicious food, but h ...
- Codeforces 1163D(kmp、dp)
要点 \(dp[i][j][k]\)表示主串已经到第\(i\)位时,\(s\)匹配在\(j\)位.\(t\)匹配在\(k\)位的最大得分 本来就要试填一层循环,如果转移也写在循环里的化复杂度承受不了, ...
- Codeforces 1154G(枚举)
我预处理\(1e7log(1e7)\)的因数被T掉了,就不敢往这个复杂度想了--无奈去看AC代码 结果怎么暴举gcd剪一剪小枝就接近3s卡过去了!vector有锅(确信 const int maxn ...
- Hive_Hive的数据模型_分区表
Hive的数据模型之分区表 准备数据表: create table sampledata (sid int, sname string, gender string, language int, ma ...
- 关于float和double类型能表示的数据范围和精度分析
来自教材<计算机组成原理>p16 float:6--7位 double:15--16位 意思就是double类型的数据,你确实能表达出很大的数字,但是其只有15位是精确的. 1.计算机中, ...
- @Primary 使用
造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...