SpringBoot通过dubbo-spring-boot-starter整合Dubbo出现的问题

本文解决了一个由于Spring版本不一致导致的异常问题,通过排除特定版本的依赖来避免冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常信息org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.servlet.handler.AbstractHandlerMapping.obtainApplicationContext()Lorg/springframework/context/ApplicationContext;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:589) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at com.ixiongyu.DubboConsumerStart.main(DubboConsumerStart.java:25) [classes/:na]
Caused by: java.lang.NoSuchMethodError: org.springframework.web.servlet.handler.AbstractHandlerMapping.obtainApplicationContext()Lorg/springframework/context/ApplicationContext;
at org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors(AbstractHandlerMapping.java:269) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMapping.initApplicationContext(AbstractHandlerMapping.java:243) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:77) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97) ~[spring-context-4.3.16.RELEASE.jar:4.3.16.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:424) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 15 common frames omitted

可以明显看到Spring版本不一致,从idea的External Libraries也可以看到:
这里写图片描述
这里写图片描述

排除这个依赖就是了,代码如下:
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>

<think>嗯,用户的问题是关于整合dubbo-spring-boot-starter和jasypt-spring-boot-starter时无法正确读取明文的问题。我需要先理解这两个库的作用以及它们为什么会冲突。 首先,dubbo-spring-boot-starterDubboSpring Boot集成的启动器,用于简化分布式服务的开发。而jasypt-spring-boot-starter则是用来加密配置文件中的敏感信息,比如数据库密码,它在应用启动时自动解密这些加密的值。 用户遇到的问题可能是当两者一起使用时,Dubbo的配置项(如注册中心地址)中的加密值没有被正确解密,导致连接失败。这可能是因为这两个库的Bean加载顺序有问题,或者Jasypt的解密处理器没有在Dubbo配置加载之前生效。 接下来,我需要回忆Spring Boot的启动流程,尤其是Bean的加载顺序和属性解析的机制。Spring Boot在启动时会先加载application.properties或application.yml中的配置,然后创建相应的Bean。如果Dubbo的配置在加载时,Jasypt的解密处理器还没有准备好,那么加密的配置项就无法被解密,导致Dubbo使用原始的加密字符串去连接注册中心,自然会失败。 可能的解决方案包括: 1. 确保Jasypt的配置正确,比如加密前缀和后缀是否正确,密钥是否设置正确。 2. 检查配置文件的加载顺序,确保Jasypt的解密处理器在Dubbo配置加载之前已经初始化。 3. 使用@DependsOn注解强制Dubbo的Bean依赖于Jasypt的解密处理器,确保解密处理器先被加载。 4.Dubbo的配置中使用明文占位符,如${jasypt.decrypt(加密字符串)},但需要确认Jasypt是否支持这种写法,或者是否需要自定义解析器。 5. 手动解密Dubbo的配置项,例如在@Configuration类中,通过Environment对象获取加密的配置值,然后手动解密后再设置到Dubbo的配置Bean中。 需要验证这些方法的可行性。比如,使用@DependsOn可能有效,因为可以控制Bean的初始化顺序。而手动解密可能需要编写额外的代码,但更灵活。 另外,参考用户提供的引用内容,特别是引用[3]中的@EnableDubbo注解,可能需要确保在启用Dubbo之前,Jasypt已经完成了配置的解密。或者检查是否有属性源(PropertySource)的顺序问题,导致Dubbo的配置没有被正确覆盖或解密。 还要考虑Jasypt的版本是否与Spring Boot版本兼容,是否有已知的冲突问题。有时候升级其中一个依赖可以解决冲突。 总结可能的解决步骤: 1. 检查Jasypt配置是否正确,包括密钥和加密格式。 2. 调整Bean的加载顺序,确保Jasypt先于Dubbo加载。 3. 使用@DependsOn注解。 4. 手动解密配置项。 5. 检查依赖版本兼容性。 现在需要将这些步骤以清晰的方式呈现给用户,并给出具体的代码示例和配置示例,同时生成相关问题,帮助用户进一步排查可能的问题。</think>### 整合dubbo-spring-boot-starter与jasypt-spring-boot-starter的明文读取问题解决方案 #### 问题原因分析 1. **加载顺序冲突** Jasypt的解密处理器可能未在Dubbo配置加载前初始化,导致加密值未被解密。例如,Dubbo的注册中心地址配置项(如`dubbo.registry.address=ENC(加密字符串)`)可能直接读取到加密字符串[^2]。 2. **属性解析机制差异** Jasypt通过`@EnableEncryptableProperties`动态解密配置,而Dubbo可能提前读取了未解密的原始值[^3]。 --- #### 具体解决步骤 ##### 1. **验证Jasypt基础配置** 确保Jasypt的加密前缀/后缀和密钥正确: ```yaml jasypt: encryptor: password: your_secret_key # 需通过环境变量或启动参数传递 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator ``` ##### 2. **调整Bean加载顺序** 在Dubbo配置类中添加`@DependsOn`强制依赖Jasypt解密处理器: ```java @Configuration @DependsOn("encryptablePropertySourceProcessor") // 确保Jasypt先初始化 public class DubboConfig { // Dubbo的@Bean定义 } ``` ##### 3. **手动解密Dubbo配置项** 在自定义配置类中主动解密加密值: ```java @Configuration public class CustomDubboConfig { @Autowired private Environment env; @Bean public RegistryConfig registryConfig() { String encryptedAddress = env.getProperty("dubbo.registry.address"); String decryptedAddress = decrypt(encryptedAddress); // 调用Jasypt解密方法 RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress(decryptedAddress); return registryConfig; } } ``` ##### 4. **检查属性源覆盖顺序** 确保Jasypt的属性源优先级高于Dubbo配置: ```properties # 在application.properties中显式指定顺序 spring.config.location=classpath:/application.yml ``` --- #### 验证流程 1. 启动时添加Jasypt密钥参数: `java -jar app.jar -Djasypt.encryptor.password=your_key` 2. 检查Dubbo日志输出: ```log [DUBBO] RegistryConfig using address: nacos://127.0.0.1:8848 # 确认是解密后的明文 ``` --- #### 配置示例 ```yaml # application.yml dubbo: registry: address: ENC(加密字符串) # Jasypt加密后的值 protocol: name: dubbo port: 20880 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值