I have an issue with Spring, I have to load a system property using an annotation.
I'm trying this approach:
@Value("${mySystemProperty}")
private String mySystemPropertyValue;
But when I make this:
System.out.println("mySystemPropertyValue="+mySystemPropertyValue);
System.out.println("system.mySystemProperty="+System.getProperty("mySystemProperty"));
It returns:
mySystemPropertyValue=null
system.mySystemProperty=myValue
What's wrong?
Thanks
EDIT
I'm trying all, but I always get in return a null value for every System property.
I also tried:
@Autowired
private Environment environment;
But the "environment" variable is null...
Try something like :
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true)
public class SecurityContextConfig extends WebSecurityConfigurerAdapter
{
@Value("#{systemProperties['your.system.property']}")
private String property;
...
}
It seems your configuration class contains some BeanFactoryPostProcessor
. I beleive it is PropertySourcesPlaceholderConfigurer
as you need this bean to have properties resolved.
Some explanation from Spring javadoc:
Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static.
That is why @Autowired
and @Value("#{systemProperties['your.system.property']}")
don't work within the config class.
PropertySourcesPlaceholderConfigurer
static or add such method if you don't have it
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
return new PropertySourcesPlaceholderConfigurer();
}
And make sure that there is no more non-static BFPP-returning @Bean methods.
Also you can move BFPP beans to separate @Configuration
class.
@Value
within @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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With