配置Git仓库的Spring Cloud Config Server启动失败求助
Spring Cloud Config Server启动报错:BeanNotOfRequiredTypeException(MultipleJGitEnvironmentProperties代理类型不匹配)
环境信息
- Spring Boot v2.6.12
- Spring Cloud Config Server v3.1.1
配置文件(application.yml)
spring: application: name: config-server main: allow-bean-definition-overriding: true cloud: config: server: git: uri: git@github.aus.thenational.com:demo/case_configuration.git skip-ssl-validation: true default-label: master clone-on-start: true clone-submodules: true force-pull: true refresh-rate: 60 search-paths: - case/folder1 - case/folder2
启动异常日志
2022-12-21 09:51:10.827 WARN 11524 --- [main] [CID:] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthContributorRegistry' parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration$ConfigServerActuatorConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searchPathCompositeEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/CompositeConfiguration.class]: Unsatisfied dependency expressed through method 'searchPathCompositeEnvironmentRepository' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'defaultEnvironmentRepository' parameter 1; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'multipleJGitEnvironmentProperties' is expected to be of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties' but was actually of type 'com.sun.proxy.$Proxy140' *************************** APPLICATION FAILED TO START *************************** Description: The bean 'multipleJGitEnvironmentProperties' could not be injected because it is a JDK dynamic proxy The bean is of type 'com.sun.proxy.$Proxy140' and implements: org.springframework.cloud.config.server.support.HttpEnvironmentRepositoryProperties org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties org.springframework.aop.SpringProxy org.springframework.aop.framework.Advised org.springframework.core.DecoratingProxy Expected a bean of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties' which implements: Action: Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
解决方案
核心问题是Spring默认生成JDK动态代理(基于接口),但MultipleJGitEnvironmentProperties是具体类,注入时按类类型匹配失败。以下是可行解决方法:
方法1:全局开启CGLIB代理
在application.yml中添加AOP配置,强制Spring使用基于类的CGLIB代理:
spring: aop: proxy-target-class: true
方法2:针对注解开启CGLIB代理
如果项目使用@EnableAsync或@EnableCaching注解,在注解上添加proxyTargetClass=true:
@SpringBootApplication @EnableConfigServer @EnableCaching(proxyTargetClass = true) // 或@EnableAsync(proxyTargetClass = true) public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
方法3:检查自定义AOP配置
如果存在自定义切面或AOP相关配置,确保全局代理模式设置为CGLIB,避免局部配置覆盖全局规则。
原理说明
JDK动态代理仅针对实现接口的类生成代理对象,代理对象类型为接口类型;而CGLIB代理直接生成目标类的子类作为代理对象,类型与目标类一致。开启proxyTargetClass=true后,Spring会优先使用CGLIB代理,确保MultipleJGitEnvironmentProperties的代理对象能被正确注入。
内容的提问来源于stack exchange,提问作者Mayank Gupta
相关产品推荐
相关产品推荐

