0

I am getting null value when trying to inject it using @Value injection in clas annotated by @Service Class but i am able to get values for same properties in class annotated by @Service I am using Spring 4.2 vesion

web.xml is as follow:::

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name></display-name>



<servlet>
    <servlet-name>CaptchaServlet</servlet-name>
    <servlet-class>com.cdac.sikkimSprings.servlets.CaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CaptchaServlet</servlet-name>
    <url-pattern>/captcha.jpg</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>SpringController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/beans.xml
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>
<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>

</web-app>

springMVC.xml is as follow

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

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

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <context:property-placeholder location="classpath:properties/development.properties" order="1"/>

    <bean id="multipartResolver"   
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

</beans>        

i also have beans.xml for bean definitions which is as follow

<?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
        name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" 
        value="root" /> <property name="password" value="password" /> </bean> -->

//Bean definition mentioned below is the same class where i need the property values to be injected which is annotated by @Service annotation

    <bean id="userManagement"
            class="com.basepackage.userManagement">

    </bean>



</beans>

Below is actual java class where values are needed to be injected

@Service("userManagementService")
@Transactional
public class userManagement implements RetrieveUserListService{

    //This value is not getting injected
    @Value("${registrationstatus_registered}")
    String statusRegistered;


    @Value("${registrationstatus_pending}")
    String statusPending;


    @Override
    public List<ShowUserListPOJO> RetrieveUserList(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub

        //here the required value is null
        System.out.println(statusRegistered);



        return null;
    }
    //Code bellow is skipped...

Same values i am injecting in class below here they are injected properly

@Controller
public class UserManagement {

    @Value( "${registrationstatus_registered}" )
    String registrationstatus;



    @RequestMapping(value="/userManagement", method = RequestMethod.GET)
    public void userManagement(HttpServletRequest request,HttpServletResponse response) {

        //here value is prnted properly
        System.out.println(registrationstatus);


       //......Code below skipped
}

development.properties is as follow

registrationstatus_registered = registered
registrationstatus_pending = pending
Rohit
  • 2,132
  • 1
  • 15
  • 24
BZT
  • 81
  • 1
  • 2
  • 10

1 Answers1

0

In your application you have two spring contexts: root application context (ContextLoaderListener) and web application context(DispatcherServlet). You need to understand the difference between them. Read here . If you move your property resource definition to beans.xml(which is used for root context), your property value will be injected correctly throughout the application.

move this bean difinition to beans.xml <context:property-placeholder location="classpath:properties/development.properties" order="1"/>.

Rohit
  • 2,132
  • 1
  • 15
  • 24
  • as i am totally new to spring can u please elaborate which resource definition are you talking about what all things i have to move to beans.xml – BZT May 26 '18 at 23:36
  • Can you share the new beans.xml file? – Rohit Jun 04 '18 at 09:41