Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.2 @value annotation with pure java configuration does not work, but Environment.getProperty works

Tags:

java

spring

I've been breaking my head on this one. Not sure what I am missing. I am unable to get the @Value annotations to work in a pure java configured spring app(non web)

@Configuration @PropertySource("classpath:app.properties") public class Config {     @Value("${my.prop}")      String name;      @Autowired     Environment env;      @Bean(name = "myBean", initMethod = "print")     public MyBean getMyBean(){          MyBean myBean = new MyBean();          myBean.setName(name);          System.out.println(env.getProperty("my.prop"));          return myBean;     } } 

The property file just contains my.prop=avalue The bean is as follows:

public class MyBean {     String name;     public void print() {         System.out.println("Name: " + name);     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     } } 

The environment variable prints the value properly, the @Value does not.
avalue
Name: ${my.prop}

The main class just initializes the context.

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); 

However if I use

@ImportResource("classpath:property-config.xml") 

with this snippet

<context:property-placeholder location="app.properties" /> 

then it works fine. Of course now the enviroment returns null.

like image 587
Abe Avatar asked Jun 13 '13 21:06

Abe


People also ask

What does @value annotation do in spring boot?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.

How does @value work in spring?

@Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument. It is commonly used for injecting values into configuration variables - which we will show and explain in the next part of the article.

Can I use @value in pojo?

For the record, the specification of what a POJO is, means that the class cannot contain pre-specified annotations. So even in another world where @Value could work on a non-spring bean, it would still, by definition, break the POJO aspect of the class.

Can we use @value in interface?

No, this is not (directly) possible. The default value of an annotation property must be a compile-time constant.


1 Answers

Add the following bean declaration in your Config class

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

In order for @Value annotations to work PropertySourcesPlaceholderConfigurer should be registered. It is done automatically when using <context:property-placeholder> in XML, but should be registered as a static @Bean when using @Configuration.

See @PropertySource documentation and this Spring Framework Jira issue.

like image 142
dimchez Avatar answered Oct 17 '22 15:10

dimchez