【问题标题】:How to load a System property in Spring using annotations? [duplicate]如何使用注释在 Spring 中加载系统属性? [复制]
【发布时间】:2016-08-09 23:42:27
【问题描述】:

我遇到了 Spring 的问题,我必须使用注释加载系统属性。

我正在尝试这种方法:

@Value("${mySystemProperty}")
private String mySystemPropertyValue;

但是当我做这个时:

System.out.println("mySystemPropertyValue="+mySystemPropertyValue);
System.out.println("system.mySystemProperty="+System.getProperty("mySystemProperty"));

返回:

mySystemPropertyValue=null
system.mySystemProperty=myValue

怎么了?

谢谢

编辑

我正在尝试所有方法,但我总是为每个系统属性返回一个空值。

我也试过了:

@Autowired
private Environment environment;

但“环境”变量为空...

【问题讨论】:

  • System.out 放在哪里?也是 Spring 托管 bean 上的 @Value 或只是一个普通的东西。
  • 我尝试了@Value("#{systemProperties['mySystemProperty']}") 和@Value("#{systemProperties.mySystemProperty}"),但我总是得到 null 作为回报。跨度>
  • 我的属性名称有一个“-”字符(确切的名称是“my-property”)。但我不认为这取决于它......

标签: java spring spring-mvc


【解决方案1】:

尝试类似:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true)
public class SecurityContextConfig extends WebSecurityConfigurerAdapter
{
    @Value("#{systemProperties['your.system.property']}") 
    private String property;
    ...
}

【讨论】:

  • 试过了,还是不行。
【解决方案2】:

您的配置类似乎包含一些BeanFactoryPostProcessor。我相信它是PropertySourcesPlaceholderConfigurer,因为您需要这个 bean 来解析属性。

来自 Spring javadoc 的一些解释:

必须特别注意返回 Spring BeanFactoryPostProcessor (BFPP) 类型的 @Bean 方法。因为 BFPP 对象必须在容器生命周期的早期实例化,它们可能会干扰 @Configuration 类中的 @Autowired、@Value 和 @PostConstruct 等注解的处理。为避免这些生命周期问题,请将返回 BFPP 的 @Bean 方法标记为静态。

这就是为什么@Autowired@Value("#{systemProperties['your.system.property']}") 在配置类中不起作用的原因。


因此,要解决您的问题,只需制作返回 PropertySourcesPlaceholderConfigurer 静态的方法,如果没有,请添加此类方法
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
    return new PropertySourcesPlaceholderConfigurer();
}

并确保不再有返回非静态 BFPP 的 @Bean 方法。 您还可以将 BFPP bean 移动到单独的 @Configuration 类。


更新
简单的应用程序演示了@Value@Configuration 中的用法
@Configuration
public class SimpleJavaConfig {

    @Value("${java.version}")
    private String property;

    public static void main(String[] args) throws IOException {
        ApplicationContext app = new AnnotationConfigApplicationContext(SimpleJavaConfig.class);
        System.out.println("|" + app.getBean("propertyBean") + "|");
    }

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

    @Bean
    public String propertyBean() {
        return property;
    }
}

【讨论】:

  • 感谢您的回答,但我不明白如何使用 bean PropertySourcesPlaceholderConfigurer 读取属性。你能告诉我一个想法吗?谢谢
  • @AlessandroC 你不需要对PropertySourcesPlaceholderConfigurer 做任何特别的事情。只需将其添加到上下文中,它就会解析属性值。需要此 bean 才能使 @Value("${mySystemProperty}") 工作。
  • 我已经按照您的指示添加了 bean,但是如果我尝试使用 @Value("${my-property}") 创建一个变量,它仍然返回 null...我没有明白为什么!
  • @AlessandroC 你是否以某种方式传递了my-property 值?例如-Dmy-property=myValue
  • @AlessandroC 我在答案中添加了简单的示例。试试看
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 2014-11-26
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
相关资源
最近更新 更多