如何使用注释在 Spring 中加载系统属性?

Ale*_*o C 4 java spring spring-mvc

我有 Spring 问题,我必须使用注释加载系统属性。

我正在尝试这种方法:

@Value("${mySystemProperty}")
private String mySystemPropertyValue;
Run Code Online (Sandbox Code Playgroud)

但是当我做这个时:

System.out.println("mySystemPropertyValue="+mySystemPropertyValue);
System.out.println("system.mySystemProperty="+System.getProperty("mySystemProperty"));
Run Code Online (Sandbox Code Playgroud)

它返回:

mySystemPropertyValue=null
system.mySystemProperty=myValue
Run Code Online (Sandbox Code Playgroud)

怎么了?

谢谢

编辑

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

我也试过:

@Autowired
private Environment environment;
Run Code Online (Sandbox Code Playgroud)

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

She*_*nar 5

尝试类似:

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