声明:本文使用的spring boot 版本是2.7.12
在springboot中使用spring-cloud-starter-openfeign遇到的一些问题:
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
java.lang.IllegalStateException: Failed to load ApplicationContext
Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
最终解决方案:
上述问题主要原因是spring boot 与spring boot的版本不兼容出现的问题
1、pom.xml文件导入正确的依赖
本文使用的spring boot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
增加 <spring-cloud.version>2021.0.9</spring-cloud.version>:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>2021.0.9</spring-cloud.version>
</properties>
增加dependencyManagement:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
增加自己要用的:
<!--http请求-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
说明:使用<spring-cloud.version>2021.0.9</spring-cloud.version>2021.0.9版本的原因:
https://docs.spring.io/spring-cloud/docs/2021.0.9/reference/html/
spring 官网说spring cloud2021.0.9版本基于支持的spring boot版本为2.6.15,然后其他版本高于本文使用的2.7.x版本,所以选择spring cloud2021.0.9。
2、导入注解
@EnableFeignClients
无需加入数据库自动注解→exclude= {DataSourceAutoConfiguration.class}
否则会出现错误Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
参考:
1、版本兼容问题:https://blog.csdn.net/joshua317/article/details/120422180
2、IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required问题:https://zhuanlan.zhihu.com/p/523730118