解决@Autowired注入static接口的问题
作者:happy_Pinocchio 时间:2022-01-02 03:16:29
@Autowired注入static接口问题
@Autowired自动注入普通service很方便
如:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private IDScypWarningMatterService warningMatterService;
public void dScypWarningMatterFZ(){
}
}
但是如果注入static修饰的service则注入不了
如:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private static IDScypWarningMatterService warningMatterService;
public static void dScypWarningMatterFZ(){
}
}
后来网上百度了一下,看到了一个方法
修改后代码如下:
@Component
public class WarningMatterUtil {
//报警表
@Autowired
private IDScypWarningMatterService warningMatterService;
private static WarningMatterUtil warningMatterUtil;
@PostConstruct
public void init() {
warningMatterUtil = this;
warningMatterUtil.warningMatterService = this.warningMatterService;
}
/**
* 赋值方法
*
*/
public static void dScypWarningMatterFZ(){
}
}
标签解释
@Autowired
:作用于构建器、属性、方法。按byType自动注入。(@Resource默认按 byName自动注入)@PostConstruct
:被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。(PreDestroy()方法在destroy()方法执行执行之后执行)
导致@Autowired注入失败的问题
背景
在一次测试当中,发现 @Autowired 无法注入的问题,使用service时,发现为null!
原因
总结了一些会导致@Autowired无法注入的原因:
@Autowired
private static TestService testService;
被static所修饰,Spring依赖注入是依赖set方法,而set方法是普通的对象方法,static变量是类的属性,所以无法完成注入操作
@Autowired按type进行自动注入,简单说就是,private TestService testService,是按照TestService 类型进行匹配注入,name可以不一致,如果匹配不到对应的类型,也会出现注入失败。
一般来说就是没有被ioc容器进行管理,在容器中匹配不到type,需要注解(@Service,@repository,@Component等)纳入spring IOC容器中
还有一种情况,当所有注解正常,可能是因为SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!“Application类”是指SpringBoot项目入口类。
这个类的位置很关键:
如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!即, 把Application类放到dao、service所在包的上级,com.boot.Application知道这一点非常关键位置不同,扫描不到对应位置下的注解。
来源:https://blog.csdn.net/happy_Pinocchio/article/details/50857917
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis实现Mapper动态代理方式详解
SpringBoot返回多种格式的数据的实现示例
![](https://img.aspxhome.com/file/2023/0/59700_0s.png)
使用Springboot 打jar包实现分离依赖lib和配置
![](https://img.aspxhome.com/file/2023/7/76537_0s.png)
Springboot如何利用拦截器拦截请求信息收集到日志详解
深入解析kafka 架构原理
![](https://img.aspxhome.com/file/2023/7/75457_0s.jpg)
Java实现Timer的定时调度函数schedule的四种用法
C#识别出图片里的数字和字母
![](https://img.aspxhome.com/file/2023/0/78180_0s.png)
mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
Java建造者设计模式详解
![](https://img.aspxhome.com/file/2023/6/71236_0s.jpg)
探究Java常量本质及三种常量池(小结)
![](https://img.aspxhome.com/file/2023/8/83058_0s.png)
Spring缓存注解@Cacheable @CacheEvit @CachePut使用介绍
使用mybatis插件PageHelper实现分页效果
![](https://img.aspxhome.com/file/2023/1/65271_0s.jpg)
JAVA 获取系统当前时间实例代码
Java实战之飞翔的小鸟小游戏
![](https://img.aspxhome.com/file/2023/3/63483_0s.png)
采用C#实现软件自动更新的方法
一篇文章看懂Java字符串操作
![](https://img.aspxhome.com/file/2023/2/71702_0s.png)
Java实现Redis的集合(set)命令操作
![](https://img.aspxhome.com/file/2023/4/61954_0s.jpg)
intellij idea如何将web项目打成war包的实现
![](https://img.aspxhome.com/file/2023/0/75520_0s.png)
Java实现LeetCode(1.两数之和)
WinForm拖拽控件生成副本的解决方法
![](https://img.aspxhome.com/file/2023/5/76385_0s.png)