springboot 在@Autowire无法注入bean时的临时处理办法
2017-10-04 12:50
507 查看
问题由来:
在用netty写一个网络编程项目的时候,需要用到mongoTemplate这个springdata提供的mongodb的操作类。但是这个只能由springboot来注入,但是netty又会进行new操作,使得@Autowire无法正常工作。解决方案:
1.我看网上的教程,有spring.xml来显式注入的,但这个有点麻烦。2.还有几个教程没看懂 比如 http://blog.csdn.net/zhousenshan/article/details/75807999
3.我最后用的方式,在一层调用的时候使用静态方法static,当然static属性和方法是不能直接使用@Autowire注入的,这里用来一个@PostConstruct来进行操作。
就是在bean初始化时运行。
@Autowired private PositionDao positionDao; private static PositionDao positionDaoStatic; @PostConstruct public void init(){ positionDaoStatic=positionDao; }
@PostConstruct 注解是在bean被构造时,先运行这个方法。
这样下面的static方法就可以使用@Autowire的属性了。而且调用静态方法也不需要new一个对象,或者使用springboot自动注入了。
另外,记得在类上加@Compact 注解。
ps:这个路子肯定是有点野的,等我研究清楚netty和spring框架可能会找到更好的方法。
相关文章推荐
- SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean
- 解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题
- Springboot整合mybatis出现无法注入mapper接口的问题处理
- SpringBoot中拦截器无法注入Bean的解决办法
- 技术文章:WAYOS 929等版本管理网页无法打开不重启处理办法
- ssh2开发中 action中无法注入业务bean解决方案
- MS SQL执行大脚本文件时,提示“未能完成操作,存储空间不足,无法处理此命令”的解决办法
- spring容器无法注入service(No qualifying bean of type)
- mybatis下无法通过service注入查询到mysql的text的字段信息的解决办法
- 移动硬盘"文件或目录损坏且无法读取"可能的处理办法
- 修改 SELINUX导致无法进入图形界面的处理办法
- 病毒或木马修改注册表,导执可执行文件无法执行的处理办法
- 使用 apt-get 的時候,出现目录或者被锁住的解决办法(E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用) E: 无法对管理目录(/var/lib/dpkg/)加锁,是不是另一个包管理)
- ora-12154 tns 无法处理服务名(执行的程序途径不一致导致,枚举网络数种方法,大家还有其他解决办法?)
- Spark集群工作异常,无法读取Hadoop集群文件处理办法
- Spring Security无法注入authenticationManager:No qualifying bean of type AuthenticationManager found for
- mybatis整合ss的时候,无法autowire使用mapper的自动注入,找不到bean:NoSuchBeanDefinitionException
- Android SDK Manager无法更新升级情况处理办法
- Android 4.x 在 https 协议下无法利用 okhttp 进行网络访问的临时解决办法
- **线程里面使用注解(线程里面spring使用注解无法注入值的解决办法)**