In this post, we will see different possible reasons why we are not able to read the value from the properties file using @Value annotation in spring.
Consider we have application.properties file in the resources folder and we want to read some properties using Spring @Value annotation. Generally we should able to read value without any trouble, but sometimes because of some code issue or configuration issue we are not read property using @Value.
Let’s see a few scenarios that might help to troubleshoot the issue.
Creating an object using a new keyword instead of Spring creates bean using @Component annotation
Suppose we have to define custom interceptor in our application. While configuring interceptor might using new keyword.
Custom interceptor class
public class RequestHandlerInterceptor extends HandlerInterceptorAdapter{
@Value("${myapp.property}")
private String myProperty;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("preHandle() is invoked");
return true;
}
}
Configuring this interceptor
public class WebAppConfigAdapter implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry) {
interceptorRegistry.addInterceptor(new RequestHandlerInterceptor () );
}
}
We can see we are using new RequestHandlerInterceptor()
. In this case we will not be able to read myProperty value from property file.
We need to autowire RequestHandlerInterceptor in WebAppConfigAdapter class and with RequestHandlerInterceptor.java we need to use @Component annotation. See here a complete example.
Apart from the above scenario there might some other reason why Spring boot @Value annotation is not working. Let’s see few of them.
The @Value
annotation in Spring is used to inject values into a Spring bean’s fields or constructor arguments. If the @Value
annotation is not working as expected, there could be several reasons:
- Incorrect import statement: Ensure that you have imported the correct
@Value
annotation from the correct package. The correct package for@Value
isorg.springframework.beans.factory.annotation.Value
. - Incorrect configuration of the application context: The
@Value
annotation works only within the Spring application context. Ensure that the class where you’re using the@Value
annotation is a Spring-managed bean and is part of the application context. - Incorrect syntax: Ensure that the syntax of the
@Value
annotation is correct. The syntax is@Value("${propertyName}")
. Make sure that the property name specified within the${}
is correct and exists in the application configuration. - Missing dependencies: The
@Value
annotation uses the Spring Expression Language (SpEL) to evaluate the property values. If the SpEL dependencies are not present, the@Value
annotation will not work. Make sure that thespring-expression
dependency is added to your project. - Incorrect property file name or location: Ensure that the property file name and location are correct. By default, Spring looks for a file named
application.properties
in the root of the classpath. If the file name or location is different, you need to specify it using the@PropertySource
annotation. - Missing
PropertySourcesPlaceholderConfigurer
bean: If you are using the@Value
annotation in a non-Spring managed bean, you need to add aPropertySourcesPlaceholderConfigurer
bean to your Spring configuration. This bean is responsible for resolving the${}
placeholders in the@Value
annotation. - Missing value in property file: If the
@Value
annotation is not working, check the property file to ensure that the property value is present and correctly spelled. If the value is not present, the@Value
annotation will not work. - The class is not a Spring-managed bean: The
@Value
annotation only works on classes that are managed by the Spring container. If the class is not a Spring bean, the@Value
annotation will not work. - The property is not defined: If the property specified in the
@Value
annotation is not defined in the configuration file, the@Value
annotation will not work. Make sure the property is defined in the correct configuration file. - The configuration file is not loaded: If the configuration file is not loaded, the
@Value
annotation will not work. Make sure the configuration file is loaded correctly. - The property is not resolved: If the property specified in the
@Value
annotation is not resolved correctly, the@Value
annotation will not work. Make sure the property is resolved correctly. - The property is not set: If the property specified in the
@Value
annotation is not set, the@Value
annotation will not work. Make sure the property is set correctly. - The
@Value
annotation is not used correctly: If the@Value
annotation is not used correctly, it will not work. Make sure the@Value
annotation is used correctly by specifying the property name and default value (if any). - The Spring version is not compatible: If the version of Spring being used is not compatible with the
@Value
annotation, it may not work. Make sure the Spring version being used is compatible with the@Value
annotation. - The
@PropertySource
annotation is not used: If the@PropertySource
annotation is not used to specify the configuration file, the@Value
annotation may not work. Make sure the@PropertySource
annotation is used to specify the configuration file.
That’s all about Spring Boot @Value not working.
Spring boot @Value annotation docs.
You may like other examples.
- Spring batch task scheduling example using spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- Spring security default authorization example using spring boot.
- Spring security inMemoryAuthentication and authorization example using spring boot.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @SpringBootApplication annotation example in Spring Boot
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @ComponentScan example in spring boot.
- Content negotiation example using Spring Boot.
- @Configuration annotation example using spring boot.