Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a System property in Spring using annotations? [duplicate]

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...

like image 551
Alessandro C Avatar asked Mar 12 '23 14:03

Alessandro C


2 Answers

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;
    ...
}
like image 100
Shekhar Khairnar Avatar answered Apr 30 '23 09:04

Shekhar Khairnar


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.


So to solve your issue just make method that returns 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.


UPDATE
Simple app demonstrating usage of @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;
    }
}
like image 45
Evgeny Avatar answered Apr 30 '23 10:04

Evgeny