在@Service注释的类中使用@Value注入时获取null值但是能够获取由@Controller注释的类中的值

时间:2018-05-26 19:38:52

标签: java spring-mvc

我尝试在@Service Class注释的clas中使用@Value注入注入它时获取null值但是我能够在@Service注释的类中获取相同属性的值 我正在使用Spring 4.2 vesion

web.xml如下:::

<?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如下

<?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>        

我也有bean.xml定义的bean.xml,如下所示

<?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定义是我需要注入属性值的同一个类  由@Service注释

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

    </bean>



</beans>

下面是需要注入值的实际java类

@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...

我正在下面的课堂注入相同的值,他们被正确注入

@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如下

registrationstatus_registered = registered
registrationstatus_pending = pending

1 个答案:

答案 0 :(得分:0)

在您的应用程序中,您有两个spring上下文:根应用程序上下文(ContextLoaderListener)和Web应用程序上下文(DispatcherServlet)。你需要了解它们之间的区别。阅读here。 如果将属性资源定义移动到beans.xml(用于根上下文),则将在整个应用程序中正确注入属性值。

将此bean定义移至beans.xml <context:property-placeholder location="classpath:properties/development.properties" order="1"/>