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);
}
}