Spring Boot创建HotelService Bean报错:No argument resolvers: afterPropertiesSet未调用
解决Spring Boot声明式HTTP客户端初始化异常的方案
1. 确认依赖是否齐全
@PostExchange这类声明式HTTP客户端注解属于Spring WebFlux功能,即便使用Servlet环境,也需要引入WebFlux依赖。
Maven的pom.xml添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
Gradle的build.gradle添加依赖:
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux'
2. 修正HotelService的Bean创建方式
不能直接将接口作为Bean返回,必须通过HttpServiceProxyFactory生成代理实例,这是声明式客户端的标准创建方式。
正确的ClientConfig示例:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.support.WebClientAdapter; import org.springframework.web.service.invoker.HttpServiceProxyFactory; @Configuration public class ClientConfig { @Bean public WebClient hotelWebClient() { return WebClient.builder() .baseUrl("https://你的外部服务基础地址") // 替换为实际服务地址 .build(); } @Bean public HotelService hotelService(WebClient hotelWebClient) { HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(hotelWebClient)) .build(); return factory.createClient(HotelService.class); } }
3. 检查HotelService接口的注解使用
接口上的@PostExchange需配合正确的参数注解,比如请求体用@RequestBody、路径参数用@PathVariable等,示例:
import org.springframework.web.service.annotation.PostExchange; public interface HotelService { @PostExchange("/hotels/create") // 替换为实际接口路径 HotelResponse createHotel(@RequestBody HotelRequest hotelRequest); }
4. 升级Spring Boot版本
你使用的3.0.0-M4是里程碑版本,存在初始化相关bug的概率较高,建议替换为3.0.0及以上的正式稳定版。
5. 确认配置类被Spring扫描到
确保ClientConfig上的@Configuration在Spring组件扫描范围内,比如放在主启动类的同包/子包下,或为主启动类添加@ComponentScan指定扫描路径。
内容的提问来源于stack exchange,提问作者mahdad aioby
相关产品推荐
相关产品推荐

