【问题标题】:How to use Spring's @Value without a properties file?如何在没有属性文件的情况下使用 Spring 的 @Value?
【发布时间】:2016-10-21 19:08:55
【问题描述】:

我目前有以下代码:

int port = System.getProperty("port") != null ?
           Integer.parseInt(System.getProperty("port")) :
           8080;

我不喜欢这个,想用 Spring 替代品来代替它。所以,我想我应该使用@Value 注释。我不想为此拥有一个属性文件。但是,我想通过注释获得一个默认值。

有没有办法在没有属性文件的情况下做到这一点?正确的代码实现是什么?我还需要PropertySourcesPlaceholderConfigurer 吗?您能否向我展示如何执行此操作的工作示例?

【问题讨论】:

  • 您能否指定需要哪个版本的春季答案?
  • 4.2.3.RELEASE,我相信....最后一个在最后一个之前。
  • 只需添加@PropertySourcesPalceholderConfigurer,添加@Value("${port:8080}"。重新启动并完成。您不需要属性文件即可使用属性源。如果您不使用 @PropertySourcesPlaceholderConfigurer,您仍然可以使用 SpEL,但这会将您仅限于系统或环境属性,并且如果您想要回退,则会变得复杂。

标签: java spring properties-file


【解决方案1】:

经过测试并且可以正常工作。

  1. 是的,您需要PropertyPlaceholderConfigurer,但它不需要属性文件
  2. 像这样引用您的系统变量:@Value("#{systemEnvironment['YOUR_VARIABLE_NAME'] ?: 'DEFAULT_VALUE'}"

代码示例:

package abc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Test {

    public final String javaPath;

    @Autowired
    public Test(@Value("#{systemEnvironment['Path']}") String javaPath) {
        this.javaPath = javaPath;
    }
}

配置:

@Configuration
@ComponentScan("abc")
public class Config {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

运行所有示例:

public class Runner {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        Test bean = context.getBean(Test.class);
        System.out.println(bean.javaPath);
    }
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    假设您使用基于 java 的配置。

    @Bean
    public static PropertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    

    然后用@Value注释一个字段

    @Value("${port:8080}")
    private int port;
    

    这将检查给定属性port 的系统属性和环境。当启用 JNDI 时,它将被检查,并且当具有基于 servlet 的环境时,您也可以将其作为 servlet 变量。

    PropertySourcesPlaceholderConfigurer 的使用不需要属性文件,它需要PropertySources,有几种不同的实现。

    如果您不想注册PropertySourcesPlaceholderConfigurer,您可以恢复为 SpEL,但这会使其更复杂(恕我直言)。

    【讨论】:

      【解决方案3】:

      我没试过,但你可以使用任何SpEL expression。您的代码可以重写为:

      @Value("#{systemProperties['port'] ?: 8080}")
      private int port;
      

      请注意,我使用的是safe navigation operator

      关于PropertySourcesPalceholderConfigurer,我认为您不需要,因为您的类路径中有 Spring 表达式语言依赖项:

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>4.2.3.RELEASE</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-23
        • 2021-12-05
        • 2021-01-27
        • 1970-01-01
        • 2011-04-01
        • 2020-12-23
        • 2017-04-11
        • 2023-02-09
        相关资源
        最近更新 更多