一.不同的包下存在同名的类名问题:
1.1 启动过程中的错误:
(类似的其他报错)
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'nameConflict' for bean class [xom.liuyun.beannameconflict.modelB.NameConflict] conflicts with existing, non-compatible bean definition of same name and class [xom.liuyun.beannameconflict.modelA.NameConflict] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:348) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:286) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:284) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:241) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:166) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE] ... 13 common frames omitted。
1.2 报错原因:
spring提供两种beanName生成策略,基于注解的sprong-boot默认使用的是AnnotationBeanNameGenerator,它生成beanName的策略就是,取当前类名(不是全限定类名)作为beanName。
由此,如果出现不同包结构下同样的类名称,肯定会出现冲突。
1.3 解决办法:
自己写一个类实现 org.springframework.beans.factory.support.BeanNameGeneraot接口
public class UniqueNameGenerator extends AnnotationBeanNameGenerator { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { //全限定类名 String beanName = definition.getBeanClassName(); return beanName; } }
在启动类上加注解@ComponentScan(nameGenerator = UniqueNameGenerator.class)使刚才我们自定义的BeanName生成策略生效。
如下:
@SpringBootApplication @ComponentScan(nameGenerator = UniqueNameGenerator.class) public class BeanNameConflictApplication { public static void main(String[] args) { SpringApplication.run(BeanNameConflictApplication.class, args); } }
二.解决Mybatis提供的@Mapper注入bean的冲突
以上第一部分可以解决@Controller,@Service等Spring提供的bean注入的同名类冲突,@Mapper不是spring的注解,所以以上部分无法解决@Mapper注入bean的类冲突
解决办法:
在启动类上@MapperScan注解加入mapper的包名
@SpringBootApplication //全限定类名 @ComponentScan(nameGenerator = UniqueNameGenerator.class) @MapperScan(value={"com.grand.p1upgrade.mapper","com.grand.quickreaction.mapper","com.grand.springboottemplate.app.mapper","com.grand.springboottemplate.environment.mapper"},nameGenerator = UniqueNameGenerator.class) //@ServletComponentScan //要将Application类放在最外侧,即包含所有子包 原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件. public class EnvironmentApplication { public static void main(String[] args) { SpringApplication.run(EnvironmentApplication.class, args); } //springcloud-前端跨域问题的解决方案全局配置 @Bean public WebMvcConfigurer corsConfigurer() { WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("*").maxAge(3600).allowedHeaders("*"); } }; return webMvcConfigurer; } }