dojacat
dojacat

Reputation: 27

@value in spring boot not giving value from application.properties

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

Upvotes: 2

Views: 1050

Answers (1)

gere
gere

Reputation: 1730

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.

Upvotes: 1

Related Questions