Spring 引导:bean 定义名称重复
Spring boot: bean definition name duplicated
我在服务开始时收到下一条消息:
ConflictingBeanDefinitionException: Annotation-specified bean name 'estatRegistreServiceImpl' for bean class [cat.gencat.catsalut.hes.mpi.api.loader.service.EstatRegistreServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [cat.gencat.catsalut.hes.mpi.api.admin.service.impl.EstatRegistreServiceImpl]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'estatRegistreServiceImpl' for bean class [cat.gencat.catsalut.hes.mpi.api.loader.service.EstatRegistreServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [cat.gencat.catsalut.hes.mpi.api.admin.service.impl.EstatRegistreServiceImpl]
目前,我创建了两个包含我的服务的包:
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/loader/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
├── EstatRegistreService.java
├── impl
├── EstatRegistreServiceImpl.java
和
src/main/java/cat/gencat/catsalut/hes/mpi/api/loader/service/
├── EstatRegistreService.java
├── impl
├── EstatRegistreServiceImpl.java
两个 bean 都使用相同的名称注册:estatRegistreServiceImpl
。
我知道我可以使用 @Service
注释设置注册名称。我不想保留这些字符串文字,因为我有很多类似服务的 类.
有没有不使用 @Service("service1") and
@Service("service2)` 的方式获取它?
谢谢
好吧,您可以创建自定义 bean 名称生成器。它将在注册 bean 时添加完整的限定符路径。
这里我已经实现了 ApplicationRunner 接口只是为了说明 bean 的名字是什么!
@SpringBootApplication
@ComponentScan(nameGenerator = YourMainApplication.CustomGenerator.class)
public class YourMainApplication extends SpringBootServletInitializer implements ApplicationRunner {
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(YourMainApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(YourMainApplication.class);
}
public static class CustomGenerator extends AnnotationBeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
return definition.getBeanClassName();
}
}
@Override
public void run(ApplicationArguments args) {
String[] allServiceBeansNames = context.getBeanNamesForAnnotation(Service.class);
for(String beanName : allServiceBeansNames) {
System.out.println(beanName);
}
}
}
我在服务开始时收到下一条消息:
ConflictingBeanDefinitionException: Annotation-specified bean name 'estatRegistreServiceImpl' for bean class [cat.gencat.catsalut.hes.mpi.api.loader.service.EstatRegistreServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [cat.gencat.catsalut.hes.mpi.api.admin.service.impl.EstatRegistreServiceImpl]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'estatRegistreServiceImpl' for bean class [cat.gencat.catsalut.hes.mpi.api.loader.service.EstatRegistreServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [cat.gencat.catsalut.hes.mpi.api.admin.service.impl.EstatRegistreServiceImpl]
目前,我创建了两个包含我的服务的包:
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/loader/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
src/main/java/cat/gencat/catsalut/hes/mpi/api/admin/service/
├── EstatRegistreService.java
├── impl
├── EstatRegistreServiceImpl.java
和
src/main/java/cat/gencat/catsalut/hes/mpi/api/loader/service/
├── EstatRegistreService.java
├── impl
├── EstatRegistreServiceImpl.java
两个 bean 都使用相同的名称注册:estatRegistreServiceImpl
。
我知道我可以使用 @Service
注释设置注册名称。我不想保留这些字符串文字,因为我有很多类似服务的 类.
有没有不使用 @Service("service1") and
@Service("service2)` 的方式获取它?
谢谢
好吧,您可以创建自定义 bean 名称生成器。它将在注册 bean 时添加完整的限定符路径。
这里我已经实现了 ApplicationRunner 接口只是为了说明 bean 的名字是什么!
@SpringBootApplication
@ComponentScan(nameGenerator = YourMainApplication.CustomGenerator.class)
public class YourMainApplication extends SpringBootServletInitializer implements ApplicationRunner {
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(YourMainApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(YourMainApplication.class);
}
public static class CustomGenerator extends AnnotationBeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
return definition.getBeanClassName();
}
}
@Override
public void run(ApplicationArguments args) {
String[] allServiceBeansNames = context.getBeanNamesForAnnotation(Service.class);
for(String beanName : allServiceBeansNames) {
System.out.println(beanName);
}
}
}