Reprinted: https://blog.csdn.net/zengfanwei1990/article/details/79924483
Foreword
- It incorporates a cache, spring boot for Project Services everything is normal.
- Shiro integrated security framework on the project and found that some kind of cache Cache is not working.
- Then find the basic failure of such notes, including transaction Transaction notes. Transactions can not function properly.
analysis
- Annotation failure classes, are (UserRealm to) used @Autowire injected class shiro frame.
- The basic determine BeanFactory shiro frame with spring framework there is a conflict, resulting in the injection shiro Framework class can not be properly initialized spring.
reference
Some articles on the site that the stackoverflow, Shiro framework initialization earlier than some parts of the Spring Framework, led to the use @Autowire inject some classes Shiro Spring framework can not be properly initialized.
Article link:
https://stackoverflow.com/questions/21512791/spring-service-with-cacheable-methods-gets-initialized-without-cache-when-autowi
Solution
- Bean Shiro is injected in a frame is not used @Autowire, using ApplicationContextRegister.getBean () method, manual injection bean. Guarantee that the method only when the program is fully up and running, was injected.
- Use @ Autowire + @ Lazy annotation provided Bean Shiro injected into the frame delay loading (i.e., loading the first time use).
Examples
The code will be problems in 1.Shiro
public class MyShiroRealm extends AuthorizingRealm { @Resource private UserInfoService userInfoService; }
2. Manual injection bean
public class MyShiroRealm the extends AuthorizingRealm { // This code only for manual injection of the bean described, omitting the code slightly before and after @Override protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection the arg0) { Long the userId = ShiroUtils.getUserId (); // where manual injection MenuService MenuService menuService ApplicationContextRegister.getBean = (MenuService. class ); the Set <String> PERMS = menuService.listPerms (the userId); SimpleAuthorizationInfo info = new new SimpleAuthorizationInfo (); info.setStringPermissions (PERMS); return info; } } 1
3. Load delay (lazy loading) (recommended)
public class MyShiroRealm extends AuthorizingRealm { @Resource @Lazy private UserInfoService userInfoService; }