在哪里将 bean 逻辑创建放入一个简单的 Spring 启动应用程序?

Where to put the bean logic creation into a simple Spring Boot application?

我已经很长时间没有使用 Java 技术了,我怀疑这是否是构建 Spring 启动应用程序的好解决方案。

我正在使用 Spring 批处理开发一个批处理应用程序到 Spring 引导应用程序(只是为了解释一般上下文,我的问题应该与 Spring 引导项目严格相关结构)。

基本上我有这个 UpdateInfoBatchApplication 是启动我的应用程序的“主要”class:

@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
public class UpdateInfoBatchApplication {
    
    @Bean
    RestTemplate restTemplate() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
        final String password = "MY_PSWD";
        final String pfxPath = "/home/andrea/Documents/workspace-spring-tool-suite-4-4.11.0.RELEASE/UpdateInfoBatch/target/classes/static/certificate.pfx";
        
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = getHttpComponentsClientHttpRequestFactory(pfxPath,password);
        
        RestTemplate restTemplate = new RestTemplate(httpComponentsClientHttpRequestFactory);
        
        return restTemplate;
        
    }
    
    

    public static void main(String[] args) {
        SpringApplication.run(UpdateInfoBatchApplication.class, args);
    }
    
    
    private static HttpComponentsClientHttpRequestFactory getHttpComponentsClientHttpRequestFactory(String pfxPath,String password) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

        SSLContext sslContext = SSLContextBuilder
                .create().loadKeyMaterial(new File(pfxPath),password.toCharArray(), password.toCharArray()).loadTrustMaterial(null, acceptingTrustStrategy).build();

        HttpClient client = HttpClients.custom().setSSLContext(sslContext).build();

        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(client);
        
        return requestFactory;
    }

}

基本上这个 class 包含启动我的 Spring 引导应用程序的 main 方法。

它还包含一个用 @Bean 注释注释的 restTemplate 方法。它创建并 return 一个自定义 RestTemplate 对象(它是调用 getHttpComponentsClientHttpRequestFactory 创建的,它本身 return 一个 HttpComponentsClientHttpRequestFactory 实例,在该实例上设置了 SSL 客户端证书需要执行一些 API 调用)。

它工作正常,但我不确定这个 UpdateInfoBatchApplication class 是放置这个 restTemplate bean 创建逻辑的好地方。

你觉得呢?可能是一个不错的解决方案还是很脏?

您应该将 bean 创建逻辑放在 class 称为“BeanConfig”或某物下。我主要根据上下文对它们进行分组,比如将密码编码器 bean 放在 SecurityBeanConfig 等中。不要忘记注释你的 class

@Configuration

将自定义可注入 bean 分离到单独的配置 classes 并确保配置 classes 的包在组件扫描的范围内总是好的。您可以重构您的代码如下

Spring 启动器 class

package com.demo;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
public class UpdateInfoBatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(UpdateInfoBatchApplication.class, args);
    }
}

配置class

package com.demo;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

@Configuration
public class UpdateInfoBatchConfiguration {

    @Bean
    @Primary
    public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory) {
        return new RestTemplate(httpComponentsClientHttpRequestFactory);
    }

    @Bean
    @Primary
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory() throws 
                            KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, 
                            KeyStoreException, CertificateException, IOException {
        final String password = "MY_PSWD";
        final String pfxPath = "/home/andrea/Documents/workspace-spring-tool-suite-4-4.11.0.RELEASE" +
                                "/UpdateInfoBatch/target/classes/static/certificate.pfx";
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = SSLContextBuilder
                            .create().loadKeyMaterial(new File(pfxPath), password.toCharArray(), password.toCharArray())
                            .loadTrustMaterial(null, acceptingTrustStrategy).build();

        HttpClient client = HttpClients.custom().setSSLContext(sslContext).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(client);
        return requestFactory;
    }
}