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
- spring 加载配置文件application.properties,类里如何调用@Value的解决办法
- 解决eclipse创建spring boot项目加载不到application.properties配置文件的问题
- spring加载properties配置文件
- Spring boot配置文件 application.properties
- Spring boot配置文件 application.properties
- Spring Boot配置文件 application.properties
- spring boot配置文件application.properties
- Spring boot配置文件application.properties
- spring boot配置文件application.properties
- spring的xml里加载properties数据
- springboot 修改默认加载的配置文件不为application.properties
- spring boot默认配置文件application.properties的属性读取
- spring boot更改配置文件 application.properties的位置
- @value读取不到数据库配置文件里的值的解决办法
- spring加载外部properties配置文件
- Spring配置文件加载properties文件
- [Spring boot]配置文件properties加载
- Spring之@Value 注解获取properties值(Spring 3的@value简化配置文件的读取)
- Git创建公共服务舱并自动同步到目标版本库
- JS中的spllit()分隔字符串和JAVA中的split分隔字符窜
- 面试题-关键字
- Windows XP、Cygwin、NDKr5b编译Android下的ffmpeg(转)
- 字段处理实例
- spring 加载配置文件application.properties,类里如何调用@Value的解决办法
- php遇到的错误与异常以及分析
- Android系统篇之----免root实现Hook系统服务拦截方法
- spring security的原理及教程
- 对3D渲染管线(渲染流水线)的理解
- 自定义ToolbarControl上的工具
- 家电产品如何创新?挖掘产品性能和市场需求是关键
- Linux命令行编辑快捷键(常用的)
- java开发微信公众号支付