Java SpringBoot整合shiro-spring-boot-starterqi项目报错解决

 更新时间:2022年08月12日 14:15:28   作者:静水楼台x  
这篇文章主要介绍了Java SpringBoot整合shiro-spring-boot-starterqi项目报错解决,文章围绕主题展开详细的内容介绍,具有一定的参考一下

1、项目启动时报错如下

Description:
The bean 'securityManager', defined in class path resource [org/apache/shiro/spring/config/web/autoconfigure/ShiroWebAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/ncwu/common/infrastructure/config/ShiroConfig.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

2、原因分析

我的自定义ShiroConfig配置类中添加的安全管理器,

代码如下:

@Bean
public SecurityManager securityManager(JwtRealm jwtRealm) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    //配置realm
    securityManager.setRealm(jwtRealm);
    return securityManager;
}

 根据异常信息查看ShiroWebAutoConfiguration源码时发现其中已经定义了securityManager方法,我们在ShiroConfig配置类中再次定义securityManager方法,因返回的类与其不一样导致出错,

ShiroWebAutoConfiguration类中定义的securityManager方法代码如下:

@Bean
@ConditionalOnMissingBean
@Override
protected SessionsSecurityManager securityManager(List<Realm> realms) {
    return super.securityManager(realms);
}

下面这些为补充知识:我们都知道@ConditionalOnBean作用是根据value属性按bean的类型或则bean的名称判断bean是否在IOC容器中,如果在就返回true,否则返回false。而@ConditionalOnMissingBean的作用与@ConditionalOnBean相反。如果@ConditionalOnBean和@ConditionalOnMissingBean这两个注解没有参数,那这两个注解以何种方式来判断呢?在Spring Boot官方文档中找出了答案。

意思是:在@ConditionalOnMissingBean没有参数的情况下,目标类型默认为方法的返回类型,如果IOC容器中没有类型为MyService及其子类的Bean,那么myServiceBean将被创建。

从源代码中可以看出@ConditionalOnMissingBean没有参数,那么如果IOC容器中没有类型为SessionsSecurityManager及其子类的Bean,那么该方法则会执行,并且源码securityManager方法返回的是SessionsSecurityManager,而自己定义的ShiroConfig中返回的是SecurityManager(因为@Bean注解会指定改bean的类型为该方法的返回类型),所以它会判断出IOC容器中没有类型为SessionsSecurityManager及其子类的Bean,源码中的方法执行,故IOC容器中有两个名为securityManager的Bean,因而报错。所以如果要自定义securityManager方法,返回类型只能是SessionsSecurityManager及其子类,而SessionsSecurityManager的子类是DefaultSecurityManager,DefaultWebSecurityManager又继承DefaultSecurityManager,

相关类图如下:

故而正确的代码应该是:

@Bean
public SessionsSecurityManager securityManager(JwtRealm jwtRealm) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    //配置realm
    securityManager.setRealm(jwtRealm);
    return securityManager;
}
 
//或者如下,将方法返回类型改为DefaultWebSecurityManager 
/*@Bean
public DefaultWebSecurityManager securityManager(JwtRealm jwtRealm) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    //配置realm
    securityManager.setRealm(jwtRealm);
    return securityManager;
}*/

3、测试@ConditionalOnMissingBean注解

新建下面3个类:

//动物
@Data
public class Animal {
    private String name;
    private Integer age;
}

//狗
@EqualsAndHashCode(callSuper = true)
@Data
public class Dog extends Animal {
    private String type;
}
 
//二哈
@EqualsAndHashCode(callSuper = true)
@Data
public class TwoHa extends Dog {
    private String a;
}

启动类:

@SpringBootApplication
//扫描下面的接口生成代理实现类
@MapperScan("com.ncwu.**.domain.mapper")
public class ShiroApplication {
    public static void main(String[] args) {
        SpringApplication.run(ShiroApplication.class, args);
    }

    //若IOC容器中没有Animal类型及其子类Dog类型的bean时,该方法才会执行
    @Bean
    @ConditionalOnMissingBean
    public Animal twoHa(JwtRealm realm) {
        TwoHa twoHa = new TwoHa();
        twoHa.setType("twoHa1");
        twoHa.setAge(10);
        twoHa.setName("twoHa1");
        return twoHa;
    }

    @Bean
    public Dog twoHa() {
        TwoHa twoHa = new TwoHa();
        twoHa.setType("twoHa2");
        twoHa.setAge(20);
        twoHa.setName("twoHa2");
        return twoHa;
    }
}

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShiroApplication.class)
public class TestDao {
    @Autowired
    private ApplicationContext appContext;
    @Test
    public void test() throws Exception{
        /*String[] beanNamesForType = appContext.getBeanNamesForType(Animal.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }*/
        appContext.getBean(Animal.class);
        //appContext.getBean("dog");
    }
}

测试结果:

到此这篇关于Java SpringBoot整合shiro-spring-boot-starterqi项目报错解决的文章就介绍到这了,更多相关Java SpringBoot 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JPA配置详解之jpaProperties用法

    JPA配置详解之jpaProperties用法

    这篇文章主要介绍了JPA配置详解之jpaProperties用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 使用springmvc的controller层获取到请求的数据方式

    使用springmvc的controller层获取到请求的数据方式

    这篇文章主要介绍了使用springmvc的controller层获取到请求的数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 关于java中Map的九大问题分析

    关于java中Map的九大问题分析

    这篇文章主要为大家详细分析了关于java中Map的九大问题,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • logback ThrowableProxyConverter类源码流程解析

    logback ThrowableProxyConverter类源码流程解析

    这篇文章主要为大家介绍了logback ThrowableProxyConverter类源码流程解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • SpringBoot项目与Nacos配置全过程

    SpringBoot项目与Nacos配置全过程

    本文介绍了如何在SpringBoot项目中使用Nacos作为配置中心,实现动态配置管理和实时更新配置的能力,通过配置命名空间和yml文件,创建SpringBoot项目并添加Nacos依赖,编写Controller和启动类,配置Tomcat启动程序,最终在Nacos服务端注册成功
    2024-11-11
  • 基于@JsonProperty的使用说明

    基于@JsonProperty的使用说明

    这篇文章主要介绍了基于@JsonProperty的使用说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • 对象转Json字符串时如何忽略指定属性

    对象转Json字符串时如何忽略指定属性

    这篇文章主要介绍了对象转Json字符串时如何忽略指定属性,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • JAVA复制数组和重置数组大小操作

    JAVA复制数组和重置数组大小操作

    这篇文章主要介绍了JAVA复制数组和重置数组大小操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot日期格式转换之配置全局日期格式转换器的实例详解

    SpringBoot日期格式转换之配置全局日期格式转换器的实例详解

    这篇文章主要介绍了SpringBoot日期格式转换之配置全局日期格式转换器的实例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • Spring Boot中Elasticsearch的连接配置原理与使用详解

    Spring Boot中Elasticsearch的连接配置原理与使用详解

    在Spring Boot中,我们可以通过Elasticsearch实现对数据的搜索和分析,本文将介绍Spring Boot中Elasticsearch的连接配置、原理和使用方法,感兴趣的可以了解一下
    2023-09-09

最新评论