Spring Boot @Value not working

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:

  1. Incorrect import statement: Ensure that you have imported the correct @Value annotation from the correct package. The correct package for @Value is org.springframework.beans.factory.annotation.Value.
  2. 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.
  3. 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.
  4. 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 the spring-expression dependency is added to your project.
  5. 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.
  6. Missing PropertySourcesPlaceholderConfigurer bean: If you are using the @Value annotation in a non-Spring managed bean, you need to add a PropertySourcesPlaceholderConfigurer bean to your Spring configuration. This bean is responsible for resolving the ${} placeholders in the @Value annotation.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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).
  14. 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.
  15. 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.