将 Spring java 配置转换为 xml 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21005762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:52:28  来源:igfitidea点击:

Convert Spring java configuration to xml configuration

javaspring

提问by Ketan

Below is my Spring Java Configuration class. I would like to have spring xml for it. The confusion I have is, how to convert @Bean HttpClient to xml (do I need to use factory method? )

下面是我的 Spring Java 配置类。我想要它的 spring xml。我的困惑是,如何将@Bean HttpClient 转换为 xml(我需要使用工厂方法吗?)

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

import javax.net.ssl.SSLContext;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.google.common.collect.Lists;

@Configuration
    public class RestClientConfig {

        @Bean
        public ClientHttpRequestFactory httpRequestFactory() throws GeneralSecurityException, IOException {
            return new HttpComponentsClientHttpRequestFactory(httpClient());
        }

        @Bean
        public HttpClient httpClient() throws GeneralSecurityException, IOException {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            TrustStrategy allTrust = new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            };

            SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            return httpClient;
        }

        @Bean
        public RestTemplate restTemplate() throws GeneralSecurityException, IOException {
            RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
            List<ClientHttpRequestInterceptor> interceptors = Lists.newArrayList();
            interceptors.add(new RestAuthInterceptor());

            restTemplate.setInterceptors(interceptors);

            return restTemplate;
        }

    }

::EDIT:: This is what I did after getting help from Jose Luis Martin and MariuszS. XML File:

::EDIT:: 这是我在得到 Jose Luis Martin 和 MariuszS 的帮助后所做的。XML文件:

<bean id="httpClient" class="com.orbit.restclient.support.CustomHttpClientFactory" />

    <bean class="org.springframework.web.client.RestTemplate">
        <constructor-arg>               
            <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
                <constructor-arg ref="httpClient" />
            </bean>
        </constructor-arg>
        <property name="interceptors">
            <list>
                <bean class="com.orbit.restclient.support.RestAuthInterceptor" />
            </list>
        </property>
    </bean>

Custom Class:

自定义类:

public class CustomHttpClientFactory implements FactoryBean<HttpClient> {

    @Override
    public HttpClient getObject() throws Exception {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        // TODO: update code here to validate certificate. This code allows all certificates
        TrustStrategy allTrust = new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
            }
        };

        SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        return httpClient;
    }

    @Override
    public Class<HttpClient> getObjectType() {
        return HttpClient.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}

采纳答案by Jose Luis Martin

Try with:

尝试:

<bean id="httpClient" class="test.HttpClientFactoryBean" />

    <bean id="httpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        <constructor-arg ref="httpClient"></constructor-arg>
    </bean>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="interceptors">
            <list>
                <bean class="RestAuthInterceptor" />
            </list>
        </property>
    </bean>

And

public class HttpClientFactoryBean extends AbstractFactoryBean<HttpClient> {

    @Override
    public Class<?> getObjectType() {
        return HttpClient.class;
    }

    @Override
    protected HttpClient createInstance() throws Exception {
         KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
         TrustStrategy allTrust = new TrustStrategy() {
             @Override
             public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                 return true;
             }
         };

         SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();

         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
         CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

         return httpClient;
    }

}

回答by MariuszS

Spring Java Configuration is much more easier to write and read, but if this is your requirement then look at something like this - this is more a concept like working solution :)

Spring Java Configuration 更容易编写和阅读,但如果这是您的要求,那么请查看类似的内容 - 这更像是一个工作解决方案之类的概念:)

Solution with Factory and Method Injection

工厂和方法注入的解决方案

<beans>

    <bean name="httpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        <constructor-arg value="httpClient"/>
    </bean>

    <bean id="httpClientFactory" class="my.custom.HttpClientFactory" scope="prototype">
        <lookup-method name="create" bean="httpClient"/>
    </bean>
    <bean name="httpClient" class="org.apache.http.client.HttpClient"/>

    <bean name="restTemplateFactory" class="my.custom.RestTemplateFactory" scope="prototype">
        <lookup-method name="create" bean="restTemplate"/>
    </bean>
    <bean name="restTemplate" class="org.springframework.web.client.RestTemplate"/>

</beans>

and example factory bean:

和示例工厂bean:

public class HttpClientFactory {

    public HttpClient create(){
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        TrustStrategy allTrust = new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        };

        SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        return httpClient;
    }

}

This is also possible to use ServiceLocatorFactoryBean

这也可以使用ServiceLocatorFactoryBean