zoukankan      html  css  js  c++  java
  • 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题

    如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现

    @Autowired

    注入的bean都是null,无法注入mapper。搜了半天似乎网上都没有相关问题,也是奇怪。最后发现

    /**
     * Shiro生命周期处理器
     *
     * @return
     */
    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    如果这个配置出现在类中,那么该类的autowired都会失效,解决办法:分离这个配置就好了

    @Configuration
    public class ShiroLifecycleBeanPostProcessorConfig {
    
        /**
         * Shiro生命周期处理器
         *
         * @return
         */
        @Bean(name = "lifecycleBeanPostProcessor")
        public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
            return new LifecycleBeanPostProcessor();
        }
    
    }

    shiro主配置

    @Configuration
    @AutoConfigureAfter(ShiroLifecycleBeanPostProcessorConfig.class)
    public class ShiroConfig {
     ...
    }

    关于这个类的描述

    1.LifecycleBeanPostProcessor用于在实现了Initializable接口的Shiro bean初始化时调用Initializable接口回调,在实现了Destroyable接口的Shiro bean销毁时调用 Destroyable接口回调。如UserRealm就实现了Initializable,而DefaultSecurityManager实现了Destroyable。具体可以查看它们的继承关系。

    2.保证实现了Shiro内部lifecycle函数的bean执行

    看得不是很懂,应该是和shiro内部bean注入和AOP有关。

  • 相关阅读:
    Mysql优化之6年工作经验总结
    mysql_innodb存储引擎的优化
    十六、MySQL授权命令grant的使用方法
    十五、Mysql字符集的那些事
    十四、索引
    十三、视图
    十二、存储过程
    十一、触发器
    十、存储引擎
    九、备份与恢复
  • 原文地址:https://www.cnblogs.com/matd/p/11128179.html
Copyright © 2011-2022 走看看