首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧引导本机配置

弹簧引导本机配置
EN

Stack Overflow用户
提问于 2022-03-21 13:57:45
回答 1查看 562关注 0票数 3

我正在尝试将现有的spring引导应用程序迁移到spring本机。在这种情况下,我有application.propertiesapplication-dev.properties

代码语言:javascript
复制
package com.test;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
import com.test.RestTemplateHeaderModifierInterceptor;

@SpringBootApplication(proxyBeanMethods = false)
public class TestApplication {

    @Value("${rest.template.connect.timeout}")
    private Integer CONNECT_TIMEOUT;

    @Value("${rest.template.connect.request.timeout}")
    private Integer CONNECT_REQUEST_TIMEOUT;

    @Value("${rest.template.read.timeout}")
    private Integer READ_TIMEOUT;

    @Value("${rest.template.max.conn.per.route}")
    private Integer MAX_CONN_PER_ROUTE;

    @Value("${rest.template.max.conn}")
    private Integer MAX_CONN;

    @Value("${slow.rest.template.read.timeout}")
    private Integer SLOW_READ_TIMEOUT;

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

    @Bean
    @Primary
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return getRestTemplateWithTimeout(READ_TIMEOUT);
    }

    @Bean("slowRestTemplate")
    public RestTemplate slowRestTemplate(RestTemplateBuilder builder) {
        return getRestTemplateWithTimeout(SLOW_READ_TIMEOUT);
    }

    private RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory requestFactory) {
        RestTemplate restTemplate = new RestTemplate(requestFactory);

        List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new RestTemplateHeaderModifierInterceptor());
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }


    private RestTemplate getRestTemplateWithTimeout(Integer readTimeout) {
        HttpComponentsClientHttpRequestFactory requestFactory = getRequestFactory(readTimeout);
        return this.restTemplate(requestFactory);
    }

    private HttpComponentsClientHttpRequestFactory getRequestFactory(Integer readTimeout) {
        HttpComponentsClientHttpRequestFactory requestFactory = getConnectionSettings();
        requestFactory.setReadTimeout(readTimeout);
        return requestFactory;
    }

    private HttpComponentsClientHttpRequestFactory getConnectionSettings() {
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(MAX_CONN_PER_ROUTE);
        connectionManager.setMaxTotal(MAX_CONN);
        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory(
                        HttpClientBuilder.create().setConnectionManager(connectionManager).build());
        requestFactory.setConnectionRequestTimeout(CONNECT_REQUEST_TIMEOUT);
        requestFactory.setConnectTimeout(CONNECT_TIMEOUT);
        return requestFactory;
    }
}

AppConfig.java

代码语言:javascript
复制
@Configuration(proxyBeanMethods = false)
@Component
@EnableAspectJAutoProxy
@EnableAutoConfiguration
@EnableRetry
public class AppConfig {
}

Config.java

代码语言:javascript
复制
@Configuration(proxyBeanMethods = false)
@PropertySource("classpath:application-${environment_region}.properties")
public class Config {

  private Map<String, String> map = new HashMap<String, String>();

  public String get(String key) {
    AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(Config.class);
    ConfigurableEnvironment env = context.getEnvironment();
    if (!map.containsKey(key) || map.get(key) == null) {
      map.put(key, env.getProperty(key));
    }
    String propertyValue = map.get(key);
    context.close();
    return propertyValue;
  }
}

建像后使用

./mvnw package -Pnative -DskipTests

当我运行应用程序时,我会得到以下错误:

代码语言:javascript
复制
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'rest.template.connect.timeout' in value "${rest.template.connect.timeout}"
        at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[na:na]
        at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[na:na]
        at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[na:na]
        at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[na:na]
        at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1330) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[na:na]
        at org.springframework.aot.beans.factory.InjectedFieldResolver.resolve(InjectedFieldResolver.java:43) ~[na:na]
        at org.springframework.aot.beans.factory.InjectedElementResolver.resolve(InjectedElementResolver.java:35) ~[na:na]
        at org.springframework.aot.beans.factory.InjectedElementResolver.invoke(InjectedElementResolver.java:53) ~[na:na]
        at com.oyo.boltkeeper.ContextBootstrapInitializer.lambda$registerBoltKeeperApplication$6(ContextBootstrapInitializer.java:15) ~[na:na]
        at org.springframework.aot.beans.factory.BeanDefinitionRegistrar$ThrowableFunction.apply(BeanDefinitionRegistrar.java:294) ~[na:na]
        at org.springframework.aot.beans.factory.BeanDefinitionRegistrar.lambda$instanceSupplier$0(BeanDefinitionRegistrar.java:115) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1249) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1191) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[na:na]
        ... 13 common frames omitted

在spring本机应用程序中传递配置的方法是什么?还是有不同的方式?这里做错什么了吗?

EN

回答 1

Stack Overflow用户

发布于 2022-07-04 14:54:07

我没有足够的声誉发表一个简单的评论:我的回答不完整.

首先,我认为您应该放弃配置文件的概念,这是在正式文件中推荐的。此外,profile的概念与提前编译是背道而驰的。如果仍然需要,文档建议在配置文件中显式指定活动配置文件。我想以spring.profiles.active=default,dev为例。

另一方面,我认为您应该在命令行中使用以下选项指定配置文件的路径:--spring.config.location=classpath:/default.properties,classpath:/override.properties。见其他文件

在“企业”上下文中,我们不包括.jar可执行文件中的配置文件:配置是外部化的。所以我认为,对于一个本土的形象来说,情况总是如此。

如果你找到了解决方案,不要犹豫分享你的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71558843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档