@Value不通过json-servlet.xml工作,而是通过applicationContext.xml工作

时间:2018-01-11 06:44:55

标签: java spring servlets

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>json</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>json</servlet-name>
        <url-pattern>/json/*</url-pattern>
    </servlet-mapping>

    <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>/json/*</url-pattern>
    </filter-mapping>

</web-app>

我的applicationContext.xml

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

    <context:component-scan base-package="com.mypackage.dao" />
    <context:component-scan base-package="com.mypackage.dao.abc" />
    <context:component-scan base-package="com.mypackage.xyz" />

    <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="ignoreResourceNotFound" value="true" />

        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
                <value>file:${user.home}/.nik/myprop.properties</value>
            </list>
        </property>
    </bean>

    <aop:aspectj-autoproxy/>

</beans>

最后我的json-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.mypackage.dao.json"/>
    <aop:aspectj-autoproxy/>

</beans>

使用上述配置,@Value内的所有com.mypackage.dao.json注释都无法解析。但@Valuecom.mypackage.daocom.mypackage.dao.abc内的所有com.mypackage.xyz注释都会成功解析。

此链接spring-web-application-context-visibility解释了原因,我理解了这一点。

但是,当我将json-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.mypackage.dao.json"/>
    <aop:aspectj-autoproxy/>

    <bean id="applicationProperties1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="ignoreResourceNotFound" value="true" />

        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
                <value>file:${user.home}/.nik/myprop.properties</value>
            </list>
        </property>
    </bean>

</beans>

@Value注释仍然无法解决。

E.g。我如何使用@Value

@Value("${myBooleanProperty}")
boolean myBooleanProperty;

@Value("${myIntegerProperty}")
Integer myIntegerProperty;

我错过了什么?我怎样才能成功解决这个问题?对引擎盖下发生的事情的详细解释将非常有用。

1 个答案:

答案 0 :(得分:0)

从此文档:https://docs.spring.io/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

以下内容应该有效:

<bean id="applicationProperties1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <!-- typed as a java.util.Properties -->
   <property name="properties">
      <value>
         myBooleanProperty=true
         myIntegerProperty=7
      </value>
   </property>
</bean>

如果要包含外部属性文件,可以通过Spring Boot项目轻松实现:

@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {

    @Value("${myBooleanProperty}")
    private boolean myBooleanProperty;
}

其他示例:https://www.mkyong.com/spring/spring-propertysources-example/