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 ...
随机推荐
- 数据库性能分析 慢查询 profile工具
- 长春理工大学第十四届程序设计竞赛(重现赛)F.Successione di Fixoracci
链接:https://ac.nowcoder.com/acm/contest/912/F 题意: 动态规划(Dynamic programming,简称dp)是一种通过把原问题分解为相对简单的子问题的 ...
- bzoj3159决战 码农题 树剖套splay
最近沉迷码农题无法自拔 首先有一个暴力的想法:对于每个重链维护一个splay,需要翻转的连起来,翻转,接回去 然后发现这样没问题... 一条链只能跨log个重链,也就只有log个splay的子树参与重 ...
- 使用Zeppelin时出现at org.apache.zeppelin.interpreter.thrift.RemoteInterpreterService$Client.recv_getFormType(RemoteInterpreterService.java:288)错误的解决办法(图文详解)
不多说,直接上干货! 问题详解 org.apache.thrift.TApplicationException: Internal error processing getFormType at or ...
- Windows2
windows如何打开dvd, iso镜像文件 .iso后缀的文件是一个压缩文件, 使用Winrar等压缩工具即可打开 windows7如何下载Visual Studio 2010(2010是流行的开 ...
- B. DZY Loves Modification
B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 洛谷P1081 开车旅行70分
https://www.luogu.org/problem/show?pid=1081 太遗憾了明明写出来了,却把最小值初始值弄小了,从第二个点开始就不可能对了.70分! #include<io ...
- 发现了一个entity framework的BUG
小弟学浅才疏可能是小题大做,但遇上了并且让我麻烦了一阵,就值得记下来 BUG的过程就是我在建立实体模型的时候 命名了一个叫system的实体模型 导致了所有生成类中 引用using system失败
- swift3.0 项目引导页
项目引导页并不难,使用 UICollectionView就可以完成, 1.首先获取应用程序的版本号,并存入本地,每次有新版本号,和存入本地的版本号,相比较 fileprivate func setup ...
- 22/tcp open|filtered ssh 80/tcp open|filtered http
22/tcp open|filtered ssh80/tcp open|filtered http nmap不能确定该端口是打开还是过滤,这可能是扫描一个打开的端口,但没有回应.