1. 程式人生 > >springmvc中的controller使用Value註解值不正確,是註解值

springmvc中的controller使用Value註解值不正確,是註解值

1,在springmvc的controller中註解屬性

public class TranOrderController extends BaseController {
    @Value("${filePath}")
    private String filePath;
    public void down(){
        logger.info(filePath); //總是列印${filePath}
    }
}

原因及解決辦法:
web.xml配置

//這個spring的配置
<context-param>
    <param-name>
contextConfigLocation</param-name> <param-value>classpath:META-INF/spring/spring-mybatis.xml</param-value> </context-param> //這個是DispatcherServlet中的配置 <servlet> <servlet-name>found</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet<
/servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:META-INF/spring/found-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping
> <servlet-name>found</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

在這2個xml檔案中要分別加入載入properties檔案的配置
spring-mybatis.xml的

<bean id="sourcePropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:dubbo.properties</value>
            <value>classpath:mq.properties</value>
            <value>classpath:console.properties</value> //這個是我要用到Valuede 
        </list>
    </property>
</bean>

found-servlet.xml的,這段配置開始的時候是沒有的,加上就沒問題了

<bean id="sourcePropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:console.properties</value> //只加了其中要用到的 //這個是我要用到Valuede 
        </list>
    </property>
</bean>