子类调用spring中注入的父类属性不生效问题

在使用shiro过程中,在启动注入时在shiroConfig中,指定了authenticationCachingEnabled=true。


    @Bean("shiroRealm")
    public ShiroRealm shiroRealm() {
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCachingEnabled(true);
        shiroRealm.setAuthenticationCachingEnabled(true);
        shiroRealm.setAuthorizationCachingEnabled(true);
        shiroRealm.setAuthenticationCacheName("authenticationCache");
        shiroRealm.setAuthorizationCacheName("authorizationCache");
        return shiroRealm;
    }

在代码中为方便使用,创建了ShiroHelper类继承ShiroRealm,封装了一些调用方法之类的。


@Component
public class ShiroHelper extends ShiroRealm {

    /**
     * 获取当前用户的角色和权限集合
     *
     * @return AuthorizationInfo
     */
    public AuthorizationInfo getCurrentuserAuthorizationInfo() {
        return super.doGetAuthorizationInfo(null);
    }

    public void clearCache(String mobile) {
        super.reloadAuthorizing(mobile);
    }

    public void clearAuthorizationCache(String mobile) {
        super.reloadAuthorizing(mobile);    }

    public void clearAuthenticationCache(String mobile) {
        soutCur();
        super.reloadAuthentication(mobile);
    }

    /**
     * 获取用户Id
     *
     * @return
     */
    public static Integer getUserId() {
        AdminUser adminUser = getUserInfo();
        if (adminUser == null) {
            return null;
        }
        return adminUser.getId();
    }

    /**
     * 获取用户对象
     *
     * @return
     */
    public static AdminUser getUserInfo() {
        return (AdminUser) SecurityUtils.getSubject().getPrincipal();
    }

}

但是在调用shiroHelper的clearCache时,发现怎么也删不掉用户的缓存。通过debug发现,shiroHelper删除缓存会判断authenticationCachingEnabled的值才会往下走,但是是false。有两种可能,1.后面某一个地方把属性修改了回来,2.不是同一个bean。1很难验证,因为在源码方法加了断点idea就超慢,启动了20分钟我放弃了。2.验证它,我在shiroRealm添加了cur属性,启动时指定为某一值。


/**
 * 自定义实现 ShiroRealm,包含认证和授权两大模块
 */
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private AdminMenuService adminMenuService;

    private String cur;//为了验证加的属性

    public void setCur(String cur) {
        this.cur = cur;
    }

    public void soutCur(){
        System.out.println(cur);
    }
//省略以下代码
}

    @Bean("shiroRealm")
    public ShiroRealm shiroRealm() {
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCachingEnabled(true);
        shiroRealm.setAuthenticationCachingEnabled(true);
        shiroRealm.setAuthorizationCachingEnabled(true);
        shiroRealm.setAuthenticationCacheName("authenticationCache");
        shiroRealm.setAuthorizationCacheName("authorizationCache");
        shiroRealm.setCur("是不是第一次注入的");
        return shiroRealm;
    }

@Component
public class ShiroHelper extends ShiroRealm {

//....

    public void clearAuthenticationCache(String mobile) {
        soutCur();
        super.reloadAuthentication(mobile);
    }

//...
}

发现直接调用shiroRealm时可以打印出“是不是第一次注入的”,调用shiroHelper打印的是null。

结论:

在spring中启动时注入的父类bean,子类并不能继承它的属性。回头想想,确实是合理的。子类和父类是两个不同的实例,继承是类之间的关系,而非实例之间的。除非是static属性。

你可能感兴趣的:(java语言)