将 http 调用程序配置导入 Spring 启动应用程序



我有一个项目,我需要使用 REST/JSON 服务公开我的服务。 我使用 Spring Boot 来开发控制器,然后通过 HTTP Invoker 代理注入服务。 这个想法是有效的,因为我已经使用基本的Spring 3.2应用程序使用XML配置完成了它。 但是,我在将 HTTP 调用程序代理导入 Spring Boot 应用程序时遇到问题。 在将相关 bean 注入控制器时,它无法找到它们。

错误(使用 Gradle 构建时的基本单元测试):

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.flashmobilecash.services.PrePaidAirtimeService za.co.flash.openapi.web.AirtimePurchaseController.airtimeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.flashmobilecash.services.PrePaidAirtimeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:522)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
    ... 59 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.flashmobilecash.services.PrePaidAirtimeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
    ... 61 more

HTTP Invoker 代理在 Java 配置中配置:

@Configuration
public class HttpServicesConfig {
    @Value("${flashtp.ipaddress}")
    private String ipAddress;
    @Value("${flashtp.httpservices.port}")
    private String port;
    @Bean
    public CloseableHttpClient httpClient() {
        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.setMaxConnPerRoute(60);
        builder.setMaxConnTotal(60);
        RequestConfig requestConfig =
            RequestConfig.custom().setConnectTimeout(10000)
                                  .setConnectionRequestTimeout(10000)
                                  .setSocketTimeout(10000)
                                  .build();
        builder.setDefaultRequestConfig(requestConfig);
        CloseableHttpClient httpClient = builder.build();
        return httpClient;
    }
    @Bean
    public HttpComponentsHttpInvokerRequestExecutor httpClientExecutor() {
        HttpComponentsHttpInvokerRequestExecutor executor = 
            new HttpComponentsHttpInvokerRequestExecutor();
        executor.setConnectTimeout(10000);
        executor.setHttpClient(httpClient());
        return executor;
    }
    .....
    @Bean
    public HttpInvokerProxyFactoryBean prePaidAirtimeService() {
        HttpInvokerProxyFactoryBean b = new HttpInvokerProxyFactoryBean();
        b.setServiceInterface(PrePaidAirtimeService.class);
        b.setServiceUrl(
            "http://" + ipAddress + ":" + port +
            "/flash-http-services/remoting/prePaidAirtimeService");
        b.setHttpInvokerRequestExecutor(httpClientExecutor());
        return b;
    }
}

然后将此配置导入到主应用程序配置中:

@Configuration
@Import(HttpServicesConfig.class)
@ComponentScan(basePackages = { "za.co.flash.openapi" })
@EnableAutoConfiguration(exclude = {
    SecurityAutoConfiguration.class,
    HibernateJpaAutoConfiguration.class
    })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

不确定我做错了什么。

问候

这种在Spring Java配置中配置HTTP调用程序的方法解决了这个问题:

@Bean
public PrePaidAirtimeService prePaidAirtimeService() {
    HttpInvokerProxyFactoryBean f = new HttpInvokerProxyFactoryBean();
    f.setServiceInterface(PrePaidAirtimeService.class);
    f.setServiceUrl(
        "http://" + ipAddress + ":" + port +
        "/flash-http-services/remoting/prePaidAirtimeService");
    f.setHttpInvokerRequestExecutor(httpClientExecutor());
    f.afterPropertiesSet();
    return (PrePaidAirtimeService) f.getObject();
}

谢谢@M Deinum为我指出正确的方向。

最新更新