In one of the services in my Spring project, I have this:
@Value("${myProject.myVar}")
private int myVar;
This is meant to reference a value in a file called application.properties
which is sitting in my ProjectRoot/src/main/java
folder. That file looks something like this:
myProject.myVar = 6
I also have a config file ProjectRoot/WebContent/WEB-INF/spring/servlet-context.xml
which contains this line:
<context:property-placeholder location="classpath:application.properties" />
When I run the project, I see the following which seems to indicate that the file is being loaded properly:
INFO : org.springframework.context.support.PropertySourcesPlaceholderConfigurer -
Loading properties file from class path resource [application.properties]
However, my program then crashes with this nested exception:
Could not autowire field: private int
org.myproject.service.MyServiceImpl.myVar; nested
exception is java.lang.IllegalArgumentException: Could not resolve placeholder
'myProject.myVar' in string value "${myProject.myVar}"
What gives?
Edit: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>Test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
/WEB-INF/spring/servlet-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/errors/error.jsp</location>
</error-page>
</web-app>