@Value not working in Spring @Configuration
Asked Answered
M

4

10

Need help, where is the issue?

I have a configuration class which is loading properties as

WebConfig.java

@Configuration
@PropertySource(value={"classpath:application.properties"})
class WebConfig extends WebMvcConfigurerAdapter{

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

I have another configuration class where I am trying to use the properties as

MyServerConfig.java

@Configuration
class MyServerConfig {

    @Value("${server.url}")
    private String url;
...
}

application.properties

server.url=http://localhost:8080/test/abc

But getting:

java.lang.IllegalArgumentException: Could not resolve placeholder 'server.url'.

Don't know what is missing here? Any thoughts?

Modulation answered 4/10, 2017 at 12:33 Comment(7)
Why did you make the method propertySourcesPlaceholderConfigurer in class WebConfig static?Pollard
Can you post your configure application.properties file.Dulcy
@Pollard Just trying one of the solution that I found in google, and it is not working even if you remove the static or completely remove the methodModulation
@SamDev Its already in the question, check againModulation
Take a review this URL #41165483Dulcy
Do you have mixed XML/Java config? If so, check that you don't have a context:property-placeholder left.Roughen
Please take a look at Minimal, Complete, and Verifiable Examples. Your problem can only be reproduced with all necessary information. At least your spring versionSowder
K
0

Use the @PropertyScan annotation in the class where a certain property will be used:

@Configuration
@PropertyScan("classpath:application.properties")
class MyServerConfig {

    @Value( "${server.url}" )
    private String url;
}
Kaycekaycee answered 4/10, 2017 at 12:40 Comment(4)
Any error? "Not working for me" doesn't help me either to find the solution that suits you.Kaycekaycee
And it is just not working here, whereas in a similar way it is working in @Service classModulation
Same error as mentioned in the question. java.lang.IllegalArgumentException: Could not resolve placeholder 'server.url'Modulation
I think you mean either @ComponentScan or @PropertySource because there is no annotation named @PropertyScanHydrocephalus
M
0

For getting the values for your @Value variables, the application.properties is not needed to be configured in any special way because this file is always scanned. So remove the @PropertySource annotation and PropertySourcesPlaceholderConfigurer bean.

These are used if you want to add other .properties files (e.g. constants.properties, db-config.properties).

So just remove those and try to run your application again

Very important:

  1. Make sure you scan the class that uses the @Value annotation (If your BootApplication is in some package instead of the 'main' package, add the proper @SpringBootApplication(scanBasePackages = { "com.my.project" }) annotation).

  2. Make sure your application.properties is on your classpath.

Bonus If you are using spring profiles (e.g: prod, dev), you can also have application-prod.properties and application-dev.properties files that will be scanned and included depending on which profile you are running.

Meagher answered 5/10, 2017 at 7:8 Comment(0)
S
0

You can use the Environment variable, which you can @Autowire if using @PropertySource in conjunction.

@PropertySource(value = { "classpath:application.properties" })
@Configuration
class MyServerConfig {
    
   @Autowired
   private Environment env;

   public void func(){
       String url = env.getProperty("server.url");
   }
}

I never tried to @Autowire it without using @PropertySource in the same @Configuration class, but most likely it will work.

WebConfig.java

@Configuration
@PropertySource(value={"classpath:application.properties"})
class WebConfig extends WebMvcConfigurerAdapter{

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

MyServerConfig.java

@Configuration
class MyServerConfig {
    
   @Autowired
   private Environment env;

   public void func(){
       String url = env.getProperty("server.url");
   }
}
Seriatim answered 20/5 at 8:22 Comment(0)
T
0

If you are using springboot, simply place the application.properties file in src/main/resources/ or src/main/resources/config/ directory. No need to configure WebConfig, all beans can directly reference properties using @Value.

If not using springboot, you can try the following approach:

@Configuration
@Import(WebConfig.class)
class MyServerConfig {

    @Value("${server.url}")
    private String url;
    ...
} 

Import the WebConfig configuration class in MyServerConfig to make the PropertySourcesPlaceholderConfigurer effective.

Terzetto answered 10/7 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.