static与 Autowire 的问题
autowire注入的是实例对象,如果直接在static String getJsonStrFromDb( HashMap hashMap)
中使用,则会报空指针。
那么如何解决呢?
想办法将实例对象注入到类对象就行了。解决思路,定义一个静态的属性,方便在static方法中使用,定义一个非static属性,在 init方法中进行替换。
public void init(){
rcsAccountService = rcsAccountService2;
rcsGroupService = rcsGroupService2;
}
@Slf4j
@Component
public class RcsParaInfoFromDbUtil {private static RcsAccountService rcsAccountService;private static RcsGroupService rcsGroupService;@Autowiredprivate RcsAccountService rcsAccountService2; @Autowiredprivate RcsGroupService rcsGroupService2;@PostConstructpublic void init(){rcsAccountService = rcsAccountService2;rcsGroupService = rcsGroupService2;}/*** @param hashMap* @return*/public static String getJsonStrFromDb( HashMap hashMap) {String str = "";Integer type = (Integer) hashMap.get("type");if (type == 1) {List<RcsAccount> rcsAccountList = rcsAccountService.getJsonStrInfo(hashMap);log.info("从数据库RCS_ACCOUNT查询返回的结果:", rcsAccountList);if (null != rcsAccountList) {str = new Gson().toJson(rcsAccountList);return str;}}if (type == 3) {List<RcsGroup> rcsGroupList = rcsGroupService.getJsonStrInfo(hashMap);log.info("从数据库RCS_GROUP查询返回的结果:", rcsGroupList);if (null != rcsGroupList) {str = new Gson().toJson(rcsGroupList);return str;}}return str;}
}
static与 Autowire 的问题相关推荐
- Spring Autowire自动装配(转http://lep1985520.blog.163.com/blog/static/56600480200901441338486/)
Spring Autowire自动装配 技术收藏 2009-01-14 16:13:38 阅读284 评论0 字号:大中小 订阅 Spring Autowire自动装配 在应用中,我们常常使用 ...
- 一次性讲清 Spring 常用注解 @Bean 、 @Component 、@Autowire、@Resource 的区别, 你知道吗?
本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerAdvice 是如何处理自定义异常的等等. Spring ...
- @Autowired注解能用在static属性吗?autowired注入static属性上为null
@Autowired注解能用在static属性吗? 答案是否定的,我们来测试下: 日志信息已经很明确了,表示static不能被@Autowired进行复制.为什么呢?我们现在就来一起了解其原因. 首先 ...
- 【Spring】Spring 自动注入(autowire)详解
1.概述 转载:添加链接描述 2. 手动注入的不足 [Spring]Spring 依赖注入之手动注入 上篇文章中介绍了依赖注入中的手动注入,所谓手动注入是指在xml中采用硬编码的方式来配置注入的对象, ...
- 详解Spring中@Autowire,@Value 注解实现原理
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/u013202238/article/details/107879726 本文主要基于SpringBoot-2.3.3.RELE ...
- 不一样的解决Non-static method 'xxx' cannot be referenced from a static context
背景:在做小组项目的时候出现的这个bug,小组项目是一个springboot项目,,我是在写某个模块的CreditServiceImpl时调用其他层方法时出的这个bug,我其他模块这么写是没问题的,在 ...
- Spring Autowire自动装配 ---残梦追月原创
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring学习第5篇:自动注入(autowire)详解
大家家好,我是一名网络怪咖,北漂五年.相信大家和我一样,都有一个大厂梦,作为一名资深Java选手,深知Spring重要性,现在普遍都使用SpringBoot来开发,面试的时候SpringBoot原理也 ...
- @Autowire注解的工作原理
@Autowired注解的工作原理 @Autowired注解用法 将@Autowired注解应用于构造函数,如以下示例所示 public class MovieRecommender {private ...
最新文章
- displaysettings.java_Android设置系统开机自动永不休眠
- WINCE6.0+S3C6410串口驱动
- 计算机在生命科学中的发展前景,往生命科学领域发展的职业前景
- 在java程序中使用protobuf
- Stop Googling!
- 【工具使用系列】关于 MATLAB 反馈神经网络,你需要知道的事
- java+线程安全的hash,多线程下HashMap安全问题-ConcurrentHashMap解析
- input文本框的readonly和disabled属性区别
- Navicat数据备份、linux mysql定时备份定时删除、docker内部mysql定时备份定时删除
- USB设备仿真框架设计指南——6.DSF核心模拟器
- 软件系统上线前演示剧本
- 汽车维修企业管理【10】
- java dead store_jcip Java并发编程实战源码,这本书名为《 践》有些抹杀了它的价值,其中 Develop 238万源代码下载- www.pudn.com...
- 面试5173的奇葩经历——老板与员工的博弈论
- 主干(trunk)、分支(branch )、标记(tag) 用法示例 + 图解
- VMware Workstation Pro 虚拟机搭建
- python入门---日常经验分享
- 文法二义性与语言二义性
- android 4k手机屏幕分辨率,安卓手机都4K屏了 真的有必要?
- DRILLNET 2.0------第十四章 钻具扭矩/摩阻模型
热门文章
- 合肥工业大学计算机组成原理实验报告
- MS1825 4X4 HDMI矩阵拼接、分割、无缝切换芯片
- 号称比 Chrome 快 400%,Avast 推出全新安全浏览器
- 美妆算法---人脸审美标准
- 明渠流量计比对装置是做什么用的?
- 2023年天津市大学软件学院高职升本科联合招生专业考试大纲
- 24c02 汇编语言,单片机读写24C02的汇编程序
- html源码去标签正则表达式
- Vue提示“[vue-router] Named Route ‘Home‘ has a default child route. When navigating to this named rout“
- Java 获取近七天、近六个月(包含今天,本月)