In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the name conflict of bean generated by springboot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Springboot generates a description of the bean name conflict problem
When we use springboot again, there may be java classes with the same name in different file directories. At this time, a bean name conflict error will be reported.
First of all, let's look at how springboot generates bean names.
When the value tree of the Component,Respository,Service,Controller annotation is not customized, a short bean name is generated based on the name of the class. For example: com.xyz.FooServiceImpl-> fooServiceImpl
That is, the path is ignored and a lowercase bean name is generated, so which of the above situations will lead to conflict
However, due to our usual habit of coding, we almost avoided the existence of this duplicate file, so we never encountered this error.
Solve the problem
Use a custom AnnotationBeanNameGenerator
SpringBootApplication@RestController@ComponentScan (nameGenerator = DatabasesApplication.SpringBeanNameGenerator.class) public class DatabasesApplication {public static class SpringBeanNameGenerator extends AnnotationBeanNameGenerator {@ Override protected String buildDefaultBeanName (BeanDefinition definition) {return definition.getBeanClassName ();}} public static void main (String [] args) {SpringApplication.run (DatabasesApplication.class, args);} @ GetMapping ("/ hello") public String hello () {return "hello databases!";}}
I am doing springboot to implement multiple data sources, and there is another place to use it.
Used in MapperScan annotations
@ MapperScan (basePackages = "com.databases.databases.dao.one", sqlSessionFactoryRef = "oneSqlSessionFactory", nameGenerator = DatabasesApplication.SpringBeanNameGenerator.class) Custom bean object renaming problem
If you already have an object, be careful not to repeat the name when setting the bean load.
Otherwise, you will be prompted to add spring.main.allow-bean-definition-overriding=true to the configuration file, which means overwriting the last one when there is a bean with the same name. It is not recommended to use it. After all, each bean object is unique.
This is the end of the content of "how to resolve bean name conflicts generated by springboot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.