spring 加载配置文件application.properties,类里如何调用@Value的解决办法

来源:互联网 发布:台湾省政府 知乎 编辑:程序博客网 时间:2024/05/01 02:51

解决方法

在springmvc-servlet.xml中添加
<context:property-placeholder location="classpath:application.properties"/>

在类里调用方法:
@Value("${appId}")
private String APP_ID;
@Value("${appSecret}")
private String APP_SECRET;
@Value("${chatSecret}")
private String CHAT_SECRET;

注意事项:

在applicationContext.xml里配置的
<context:property-placeholder location="classpath:application.properties"/>
是不能在类里调用的,是因为 applicationContext和springmvc-servlet是两个不同的容器,所以相互不能调用,在springMvc-servlet.xml里添加了类扫描 <context:component-scan base-package="cn.gcks.unifiedlogin"/>
所以application.propeities也要配置进去。

springmvc-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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"       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       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd       ">    <context:property-placeholder location="classpath:application.properties"/>    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->    <context:component-scan base-package="cn.gcks.unifiedlogin"/>    <!-- 激活@Controller模式 注入sessionInfo参数 -->    <mvc:annotation-driven>        <mvc:argument-resolvers>            <bean class="cn.gcks.unifiedlogin.common.SessionInfoArgumentResolver"/>        </mvc:argument-resolvers>    </mvc:annotation-driven>    <!-- Spring MVC JSON配置 -->    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <!-- <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> -->            <!-- <bean id="fastJsonHttpMessageConverter" class="cn.com.infcn.iboss.util.FastJsonHttpMessageConverter"> -->            <bean id="mappingJacksonHttpMessageConverter"                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <!--   避免IE出现下载JSON文件的情况 -->                <property name="supportedMediaTypes">                    <list>                        <value>application/json;charset=utf-8</value>                        <value>text/html;charset=UTF-8</value>                    </list>                </property>            </bean>        </property>    </bean>    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize" value="10485760" />    </bean>    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/"          p:suffix=".jsp"/>    <!-- 拦截器 -->    <mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path="/**"/>            <mvc:exclude-mapping path="/auth/login"/>            <mvc:exclude-mapping path="/webSocketServer/**"/>            <mvc:exclude-mapping path="/auth/login/**"/>            <bean class="cn.gcks.unifiedlogin.common.SecurityInterceptor">            </bean>        </mvc:interceptor>    </mvc:interceptors></beans>

application.properties文件:

#微信企业号appId=wx2ffffffappSecret=jjjjjchatSecret=ffwefwewe
0 0
原创粉丝点击