Java SpringBoot
静水楼台x 人气:01、项目启动时报错如下
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"); } }
测试结果:
加载全部内容