環境
- shiro 1.4.0
- springboot 2.2.6
- jdk 1.8
問題復現
在使用springboot集成shiro中,在shiro中注入userServiceImpl,會導致userServiceImpl不能被spring代理,導致事務失效。
部分代碼
public class MyShiroRealm extends AuthorizingRealm {
@Autowired
UserService userService;
.............
啓動時的報錯信息如下
Bean 'userServiceImpl' of type [cn.junengxiong.service.impl.UserServiceImpl] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
解決辦法
第一種
加上 @Lazy 註解,告訴spring 懶加載此userService。
public class MyShiroRealm extends AuthorizingRealm {
@Autowired
@Lazy
UserService userService;
.............
第二種
在Realm中選擇注入dao層,而不選擇service層
public class MyShiroRealm extends AuthorizingRealm {
@Autowired
@Lazy
UserMapper userMapper;
.............