2

I am trying to read value from application properties into a class annotated with

@Configuration public class testClass { @Value("${com.x.y.z.dummy.name}") private String name; 

once I run the code at a method in this class annotated with @Bean:

 @Bean public Helper helper(X x){ System.out.println(this.name); } 

Here the output is -> ${com.x.y.z.dummy.name} instead of the value of ${com.x.y.z.dummy.name} in the application.properties . I tried @Autowired and tried reading from environment variable too. Not sure what might be happening. Could anyone help with this? Adding application.properties:

com.x.y.z.dummy.name=localhost com.x.y.z.dummy.host=8888 
9
  • 2
    If it's a Spring Boot application, it should normally work as described. If it's a Spring Core application, you should register a PropertySourcesPlaceholderConfigurer as a bean post-processor (using static keywork) Commented Feb 20, 2021 at 10:09
  • Possible duplicate stackoverflow.com/questions/25764459/… Commented Feb 20, 2021 at 10:10
  • I have tried this it dint help @fatrixienicolie Commented Feb 20, 2021 at 10:22
  • 1
    Share your codebase through github for quicker solution Commented Feb 20, 2021 at 11:07
  • 1
    @Nico Van Belle thank you ! Commented Feb 21, 2021 at 18:45

1 Answer 1

1

I would suggest to search in your project for a Bean that returns a PropertySourcesPlaceholderConfigurer. That object could be configured to set a different prefix instead of "${". Doing so would result in the behavior that you are describing.

For example, creating this class I was able to reproduce your problem.

import org.springframework.context.annotation.Bean; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; public class PrefixConfiguration { @Bean public static PropertySourcesPlaceholderConfigurer configure(){ PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setPlaceholderPrefix("%{"); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; } } 

Your Bean could be different, and it could be there for a good reason, so don't blindly delete it without further investigation.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @gere , I don't see the PropertySourcesPlaceholderConfigurer at all , I am facing this problem post an upgrade of a dependency which is a third party . Once I added the PropertySourcesPlaceholderConfigurer it seems to be working fine
Then the PropertySourcesPlaceholderConfigurer is configured inside the third party library in a way that changes the normal behavior. When you add it yourself, you are declaring it again, and doing so, make it work normally again.
Hey why should this -> static PropertySourcesPlaceholderConfigurer configure() be a static method ? Could you please help me understand ?
Because the Spring docs suggest to do so: look here for the paragraph "BeanFactoryPostProcessor-returning @Bean methods": docs.spring.io/spring-framework/docs/current/javadoc-api/org/… it explains why and it uses a bean returning exactly the PropertySourcesPlaceholderConfigurer as an example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.