当前位置: 首页 > 知识库问答 >
问题:

PropertySourcesPlaceholderConfigurer和SPEL?

柳才良
2023-03-14

由于我们相当复杂的properties-file设置,我们不能简单地使用@propertysource

connection.http.connectTimeout=15000
#connection.http.readTimeout=${connection.http.connectTimeout}
connection.http.readTimeout=#{30*1000}
@Component
@ConfigurationProperties("connection")
public class ConnectionConfig {

  @NestedConfigurationProperty
  private ConnectionSourceConfig http;

  public ConnectionSourceConfig getHttp() {
    return http;
  }

  public void setHttp(ConnectionSourceConfig http) {
    this.http = http;
  }

}

public class ConnectionSourceConfig {

  private long connectTimeout;

  private long readTimeout;

  public long getConnectTimeout() {
    return connectTimeout;
  }

  public void setConnectTimeout(long connectTimeout) {
    this.connectTimeout = connectTimeout;
  }

  public long getReadTimeout() {
    return readTimeout;
  }

  public void setReadTimeout(long readTimeout) {
    this.readTimeout = readTimeout;
  }

}
@Configuration
public class BaseAppConfig {

  @Bean
  public static PropertySourcesPlaceholderConfigurer properties(Environment environment) throws IOException {
    String env = getEnvProperty(environment);
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setLocations(getPropertiesFiles(env));
    configurer.setIgnoreResourceNotFound(true);
    return configurer;
  }
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer() {

      @Override
      protected String convertPropertyValue(String originalValue) {
        System.out.println("Parse " + originalValue);
        return super.convertPropertyValue(originalValue);
      }

    };

但是,也许我在Spring的处理生命周期上犯了一个错误...

共有1个答案

卓云
2023-03-14

属性文件中,#被认为是一个注释块。这样做,您的属性文件最终将一个属性设置为空。

connection.http.readtimeout=#{30*1000}呈现为connection.http.readtimeout=,因为#之后的所有内容都被忽略。

 类似资料:
  • 问题内容: 查看3.1(http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property- management/ )中对spring的新支持,看起来这应该是可能的: 其中login.security在application-customer-dev.properties中为: (并且security- dev.xml确实

  • 这种方法适用于xml配置 但我想使用java进行配置。

  • 问题内容: 我有以下配置类: and I have service with property: 我想使用以下命令测试AppConfig配置类时收到错误消息 该问题记录在SPR-8539中 但无论如何,我无法弄清楚如何配置PropertySourcesPlaceholderConfigurer 使其正常工作。 编辑1 这种方法适用于xml配置 但我想使用Java进行配置。 问题答案: 如果使用@P

  • 不知道最好的方法。我们已经创建了一个可供不同项目使用的jar。依赖此jar的其他项目需要提供在其spring属性文件中定义的某些属性。(我们的jar不应该关心他们给这些属性文件起什么名字。) 我不介意让这个特定的服务类成为ApplicationContextAware,但是如果我这样做了,我如何能够访问底层的PropertySourcesPlaceholderConfigurer?我会“看起来”吗

  • 我很困惑如何为外部配置设置位置, 基于https://docs.spring.io/spring-boot/docs/1.2.2.release/reference/html/boot-features-external-config.html我进行了一些外部配置,但失败了,下面是我的代码

  • 问题内容: 关于它们有很多传说。我想知道真相。以下两个示例之间有什么区别? 问题答案: 不确定从何处获得传说,但: 提交按钮 与: IE6将在标记之间提交此按钮的所有文本,其他浏览器将仅提交值。使用可使您在按钮的设计上享有更大的布局自由度。从各种意图和目的看,它乍一看似乎很棒,但是各种浏览器怪癖使它有时很难使用。 在您的示例中,IE6将发送到服务器,而其他大多数浏览器将不发送任何内容。要使其跨浏览