After spring boot integration shiro, part of the notes (Cache Cache, Transaction Services, etc.) failure problem

Reprinted: https://blog.csdn.net/zengfanwei1990/article/details/79924483

Foreword

  1. It incorporates a cache, spring boot for Project Services everything is normal.
  2. Shiro integrated security framework on the project and found that some kind of cache Cache is not working.
  3. Then find the basic failure of such notes, including transaction Transaction notes. Transactions can not function properly.

analysis

  1. Annotation failure classes, are (UserRealm to) used @Autowire injected class shiro frame.
  2. 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

  1. 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.
  2. 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;
}

 

Guess you like

Origin www.cnblogs.com/cq-yangzhou/p/11418117.html