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
@Valueannotation from the correct package. The correct package for@Valueisorg.springframework.beans.factory.annotation.Value. - Incorrect configuration of the application context: The
@Valueannotation works only within the Spring application context. Ensure that the class where you’re using the@Valueannotation is a Spring-managed bean and is part of the application context. - Incorrect syntax: Ensure that the syntax of the
@Valueannotation 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
@Valueannotation uses the Spring Expression Language (SpEL) to evaluate the property values. If the SpEL dependencies are not present, the@Valueannotation will not work. Make sure that thespring-expressiondependency 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.propertiesin the root of the classpath. If the file name or location is different, you need to specify it using the@PropertySourceannotation. - Missing
PropertySourcesPlaceholderConfigurerbean: If you are using the@Valueannotation in a non-Spring managed bean, you need to add aPropertySourcesPlaceholderConfigurerbean to your Spring configuration. This bean is responsible for resolving the${}placeholders in the@Valueannotation. - Missing value in property file: If the
@Valueannotation 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@Valueannotation will not work. - The class is not a Spring-managed bean: The
@Valueannotation only works on classes that are managed by the Spring container. If the class is not a Spring bean, the@Valueannotation will not work. - The property is not defined: If the property specified in the
@Valueannotation is not defined in the configuration file, the@Valueannotation 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
@Valueannotation will not work. Make sure the configuration file is loaded correctly. - The property is not resolved: If the property specified in the
@Valueannotation is not resolved correctly, the@Valueannotation will not work. Make sure the property is resolved correctly. - The property is not set: If the property specified in the
@Valueannotation is not set, the@Valueannotation will not work. Make sure the property is set correctly. - The
@Valueannotation is not used correctly: If the@Valueannotation is not used correctly, it will not work. Make sure the@Valueannotation 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
@Valueannotation, it may not work. Make sure the Spring version being used is compatible with the@Valueannotation. - The
@PropertySourceannotation is not used: If the@PropertySourceannotation is not used to specify the configuration file, the@Valueannotation may not work. Make sure the@PropertySourceannotation 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.


