In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to exclude Spring Boot from automatically loading data sources". 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!
Preface
Some older projects use Spring MVC with written database connection pooling, such as redis/mongodb/mybatis (the same for other Oracle in mysql). Errors are reported when these projects move into the spring boot framework.
The reason is that our business has written connection pooling, but spring boot will actively load the autoconfiguration of spring boot to create connection pooling when the jar package exists. However, we do not configure the Spring Boot parameter and do not need to configure it.
1. Mongodb
The mongodb auto configuration error is as follows:
Org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017
Com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.ConnectException: Connection refused (Connection refused)
But I didn't introduce spring-boot-starter-data-mongodb 's jar package, and later found out that I introduced spring-data-mongodb 's jar.
Check the jar of spring-boot-starter-data-mongodb, including three parts, as follows:
I have all my jar bags, which is equivalent to putting these jar together into spring-boot-starter-data-mongodb.
Automatic configuration function is automatically introduced in Spring Boot.
Need to manually exclude automatically configured data sources, exclude in SpringBootApplication
@ SpringBootApplication (exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
Start the connection localhost:27017 without error, and the business is normal. For the principle, see the Spring Boot official documentation.
2. Mybatis
Mybatis is the same.
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded data
* *
APPLICATION FAILED TO START
* *
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
Need to be excluded
@ SpringBootApplication (exclude = {DataSourceAutoConfiguration.class}) 3. Principle explanation
The principle is EnableAutoConfiguration.
Follow up further:
The AutoConfigurationImportSelector class has logic for automatic loading and exclusion.
Public String [] selectImports (AnnotationMetadata annotationMetadata) {if (! isEnabled (annotationMetadata)) {return NO_IMPORTS;} AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata (this.beanClassLoader); AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry (autoConfigurationMetadata,annotationMetadata); return StringUtils.toStringArray (autoConfigurationEntry.getConfigurations ());}
Pay attention to loading the code
GetAutoConfigurationEntry (autoConfigurationMetadata, annotationMetadata); / * * Return the {@ link AutoConfigurationEntry} based on the {@ link AnnotationMetadata} * of the importing {@ link Configuration @ Configuration} class. * @ param autoConfigurationMetadata the auto-configuration metadata * @ param annotationMetadata the annotation metadata of the configuration class * @ return the auto-configurations that should be imported * / protected AutoConfigurationEntry getAutoConfigurationEntry (AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {if (! isEnabled (annotationMetadata)) {return EMPTY_ENTRY } AnnotationAttributes attributes = getAttributes (annotationMetadata); List configurations = getCandidateConfigurations (annotationMetadata, attributes); configurations = removeDuplicates (configurations); Set exclusions = getExclusions (annotationMetadata, attributes); checkExcludedClasses (configurations, exclusions); configurations.removeAll (exclusions); configurations = filter (configurations, autoConfigurationMetadata) FireAutoConfigurationImportEvents (configurations, exclusions); return new AutoConfigurationEntry (configurations, exclusions);}
Inside
GetExclusions (annotationMetadata, attributes); / * Return any exclusions that limit the candidate configurations. * @ param metadata the source metadata * @ param attributes the {@ link # getAttributes (AnnotationMetadata) annotation * attributes} * @ return exclusions or an empty set * / protected Set getExclusions (AnnotationMetadata metadata, AnnotationAttributes attributes) {Set excluded = new LinkedHashSet (); excluded.addAll (asList (attributes, "exclude")) Excluded.addAll (Arrays.asList (attributes.getStringArray ("excludeName"); excluded.addAll (getExcludeAutoConfigurationsProperty ()); return excluded;}
See, exclude or excludeName, and of course there's another way.
Private List getExcludeAutoConfigurationsProperty () {if (getEnvironment () instanceof ConfigurableEnvironment) {Binder binder = Binder.get (getEnvironment ()); return binder.bind (PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String [] .class) .map (Arrays::asList) .orElse (Collections.emptyList ()) } String [] excludes = getEnvironment () .getProperty (PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String [] .class); return (excludes! = null)? Arrays.asList (excludes): Collections.emptyList ();}
Configure spring.autoconfigure.exclude through an application.properties file
Private static final String PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude"; "how Spring Boot excludes automatic loading of data sources" ends here. 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.