前言
- 整合有缓存、事务的spring boot项目一切正常。
在该项目上整合shiro安全框架,发现部分类的缓存Cache不能正常使用。
然后发现该类的注解基本失效,包括事务Transaction注解。事务不能正常运行。
分析
- 注解失效的类,都是在shiro框架中(UserRealm)使用过@Autowire注入的类。
基本确定是shiro框架与spring框架的BeanFactory有所冲突,导致注入shiro框架的类不能被spring正确初始化。
以上参考
https://blog.csdn.net/elonpage/article/details/78965176
我的解决方法
1.比如使用Shiro时我们自定义了Realm,并在其中使用了
@Autowired
private UserService userService;
2.现在修改为
@Autowired
@Lazy
private UserService userService;
即可,还有方法,比如ApplicationContextRegister.getBean()手动注入Bean。