首页 > 解决方案 > 在@Service注释的类中使用@Value注入时获取空值,但能够在@Controller注释的类中获取值

问题描述

我在尝试使用 @Service 类注释的 clas 中的 @Value 注入时得到空值,但是我能够在由 @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>        

我也有 beans.xml 用于 bean 定义,如下所示

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

标签: javaspring-mvc

解决方案


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

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


推荐阅读