0

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>
Ecliptica
  • 760
  • 2
  • 8
  • 21
  • Possibly related to this issue? https://stackoverflow.com/questions/11890544/spring-value-annotation-in-controller-class-not-evaluating-to-value-inside-pro – Chris Thompson Mar 07 '17 at 00:54

2 Answers2

0

Your application.properties file is in wrong location. It should be under src/main/resources for the application context to load it. If you are running test cases then it should be under src/test/resources.

Note, you are initiating spring boot application as

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
user1211
  • 1,507
  • 1
  • 18
  • 27
  • I tried this but I got the same error. I also tried just deleting `application.properties` temporarily, and I got a different error about the file not being found. It seems the file is being read, but the value isn't being mapped for some reason. – Ecliptica Mar 07 '17 at 00:22
  • How are u initializing spring boot context? – user1211 Mar 07 '17 at 00:41
  • In `web.xml`, I have `servlet-context.xml` listed as a parameter. – Ecliptica Mar 07 '17 at 00:45
  • My guess is, you might have registered PropertyPlaceholderConfigurer bean in your xml and might have used annotation driven approach too. Doing both together is not a good idea. I will suggest, using spring boot which is completely annotation driven – user1211 Mar 07 '17 at 01:36
0

Two suggestions

  1. put your properties files under WEB-INF/classes, build tools like Maven or Gradle will automatically put all files under src/main/resources into the WEB-INF/classes.

  2. remove spaces before and after the =. myProject.myVar = 6 become myProject.myVar=6

PWC
  • 266
  • 3
  • 11
  • Thanks for the suggestions—unfortunately implementing them didn't change anything. – Ecliptica Mar 07 '17 at 22:52
  • @Ecliptica Then, I afraid your must provide more detail about your project. Like `web.xml`. – PWC Mar 08 '17 at 05:57
  • @Ecliptica I suspect you maybe load the value into diff context. you can try to config you web.xml with only one context, info can be found here http://stackoverflow.com/q/9016122/1770155. – PWC Mar 08 '17 at 06:17
  • I've added my `web.xml` file to the original question. – Ecliptica Mar 09 '17 at 23:46
  • @Ecliptica 1. try to remove the listener and context-param tags and run again. 2. Can you find the `application.properties` under `/WEB-INF/classes` in the exploded folder under tomcat? – PWC Mar 10 '17 at 07:04