Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value annotation in @Controller class not evaluating to value inside properties file

I am new to Spring and trying to inject a string with a value using the @Value("${loginpage.message}") annotation inside of a controller annotated with the @Controller annotation and the value of my string is being evaluated as the string "${loginpage.message}" and not what is inside my properties file.

Below is my controller with the string 'message' that I want to inject.

@Controller public class LoginController extends BaseController {     @Value("${loginpage.message}")     private String message;      @RequestMapping("/")     public String goToLoginPage(Model model) {         model.addAttribute("message", message);          return "/login";     } } 

My application context looks like this:

<context:property-placeholder location="classpath:properties/application.properties" />  <context:annotation-config />  <context:component-scan base-package="com.me.application" /> 

My properties file has the line:

loginpage.message=this is a test message 

Spring must be picking up the value at some point because whenever I change @Value("${loginpage.message}") to a value not in the properties file like @Value("${notInPropertiesFile}"), I get an exception.

like image 976
Chris Avatar asked Aug 09 '12 19:08

Chris


People also ask

Can we use @value in controller class?

@Value annotation can be used within classes annotated with @Configuration , @Component and other stereotype annotations like @Controller , @Service etc.

Where can I use @value annotation?

Spring @Value with methods If we want different values for different arguments then we can use @Value annotation directly with the argument.

How property values can be injected directly into your beans in spring boot?

Property values can be injected directly into your beans using the @Value annotation, accessed via Spring's Environment abstraction or bound to structured objects via @ConfigurationProperties . Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values.


2 Answers

It seems that the question has been already asked Spring 3.0.5 doesn't evaluate @Value annotation from properties

The difference between web app root and servlet application contexts is one of the top sources of confusion in Spring, see Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

From @Value javadoc :

Note that actual processing of the @Value annotation is performed by a BeanPostProcessor

From Spring documentation:

BeanPostProcessor interfaces are scoped per-container. This is only relevant if you are using container hierarchies. If you define a BeanPostProcessor in one container, it will only do its work on the beans in that container. Beans that are defined in one container are not post-processed by a BeanPostProcessor in another container, even if both containers are part of the same hierarchy.

like image 103
Boris Treukhov Avatar answered Sep 22 '22 20:09

Boris Treukhov


Yea I am having same issue with Spring 3. It doesn't seem to work inside Controllers. To fix the issue I created a another bean with @Service and injected that into the controller. It did work for me. Hope this would be helpful to someone as I spent all day to figure it out.

like image 34
tn.stackoverflow Avatar answered Sep 18 '22 20:09

tn.stackoverflow