0

I am trying to read a property file in Spring mvc 4.1.6 version and I am having some issues.

Dispatcher-servlet.xml

<context:property-placeholder location="classpath:login.properties"/> 

My class

@Component
public class UserCheck {

    @Value("${server_ip}")  String server_ip;
    public String isUserPresent(String userName){
        System.out.println("server_ip: " + server_ip);

        return "";

    }
}

I am getting the server_ip as null when I print the value. My login.properties is in src/main/resources folder.

I also tried @Value("${login.server_ip}"), but then I got a different error when deploying the application. I got

org.springframework.beans.factory.BeanCreationException: Error creating bean with name : Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field

Is there anything else that I need to do?

Thanks.

EDIT:

My properties file looks like below:

server_ip=192.168.1.1

My complete dispatcher servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc   
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.company"></context:component-scan>
    **<context:annotation-config/>**
    <mvc:annotation-driven />

    <mvc:resources mapping="/**" location="/" />
    <context:property-placeholder location="classpath:login.properties"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value="/WEB-INF/jsp/" />
        <property name = "suffix" value=".jsp" />
    </bean>
    <bean class="org.springframework.context.support.ResourceBundleMessageSource"
        id="messageSource">
        <property name="basename" value="messages" />
    </bean>
This was added for testing**********************
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
          <list>
            <value>classpath:application.properties</value>
          </list>
        </property>
    </bean>
</beans>

EDIT 2:

My web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>test</display-name>
  <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

EDIT 3: My controller class with the @PostConstruct method

@Controller
@SessionAttributes("userData")
public class UserController {

    @Autowired
    @Value("${server_ip}")  
    String server_ip;
    @Value("${username}")   
    String username;



    public @ResponseBody
    @RequestMapping(value = "/validateuser")
    String validateUser(@RequestParam("user") String user){
        //UserCheck userCheck = new UserCheck();
        //String test = userCheck.isUserPresent(user);
        return "";

    }//validateUser

    @PostConstruct
    public void init(){
        System.out.println("server_ip in inti: " + server_ip);
        System.out.println("username: " + username);

    }

}
user115391
  • 185
  • 1
  • 8
  • 22
  • I dont believe this is a problem of the properties file. And you have nicely hidden which field it could not autowire. – We are Borg May 12 '15 at 15:10
  • Apologies, I did not understand what you mean. – user115391 May 12 '15 at 15:55
  • Your error log reads Could not autowire field, but you didnt include which field it did not autowire. – We are Borg May 12 '15 at 15:56
  • The logs when I add login.server_ip instead of just server_ip is 'Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.lang.String com.*.UserCheck.server_ip; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'login.server_ip' in string value "${login.server_ip}"' – user115391 May 12 '15 at 16:01
  • Please add relevant snippets from your .properties file and also show more of the application context xml file – arahant May 12 '15 at 17:02
  • I added the property information in my main post and I do not have an applicationContext.xml for my app. Thanks. – user115391 May 12 '15 at 17:17
  • Add your Dispatcher-servlet.xml, is what I meant. – arahant May 12 '15 at 17:34
  • Updated with the complete dispatcher servlet xml file. – user115391 May 12 '15 at 17:55
  • Nope, did not help. I added the config you mentioned in my dispatcher-servlet.xml, but still I get null. Thanks. – user115391 May 12 '15 at 20:04
  • Can you please edit your main post to include the error log, the line of code and its method which is problematic, and your web.xml – We are Borg May 13 '15 at 07:58
  • Thanks. I added the web.xml. Technically, I do not have an error, it just prints null. But when I add ${login.server_ip} instead of ${server_ip}, I get the error - Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.lang.String com.*.UserCheck.server_ip; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'login.server_ip' in string value "${login.server_ip}" – user115391 May 13 '15 at 12:53
  • Might be this help you: http://stackoverflow.com/questions/30128434/referencing-a-value-from-a-spring-config/30128981#30128981 – Arpit Aggarwal May 13 '15 at 13:05
  • Thanks. I added the PropertySourcesPlaceholderConfigurer in the dispatcher servlet xml file, but still only get the null value. I am trying this from an Util class if that makes any difference. I have a controller and the controller calls this method, but the values are null. – user115391 May 13 '15 at 15:52
  • Are you sure you are calling the method *after* the Dependency Injection is done? To check this add a *@PostConstruct* method in your *@Controller* and try calling your isUserPresent method from there. – arahant May 13 '15 at 18:42
  • I tried to add the @PostConstruct method in the controller, the method was not even called. I do not see the print statements in the logs. I read that I need `` and I have it in my dispatcher config xml. Do I need to add anything else? I have the controller code in EDIT 3 of my main post. Thanks. – user115391 May 14 '15 at 14:20

1 Answers1

0

First of all, you would need the @Configuration annotation on the class. Not sure it would work with just @Component

Secondly, I don't know your project type and setup but if your .properties file ends up in WEB-INF/classes directory, then Spring should be able to pick it up.

EDIT: You need to add this to your configuration file <context:annotation-config/>

arahant
  • 2,203
  • 7
  • 38
  • 62
  • I added the @Configuration, but it did not help. Also the properties file is already present in the WEB-INF/classes directory. – user115391 May 12 '15 at 15:57