注解式读取properties文件中的内容
来源:互联网 发布:js获取td的父节点tr 编辑:程序博客网 时间:2024/06/06 07:46
注解式读取properties文件中的内容:
springMvc获取properties文件的注解方式:
需要的jar包:spring-beans-4.1.4.RELEASE.jar
1.首先需要在application.xml中注入:
<!--配置注解式配置文件读取--><bean class="org.springframework.beans.factory.config.PropertiesFactoryBean" id="configProperties"> <property name="locations"> <list> <value>classpath*:app.properties</value> </list> </property></bean><bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" id="propertyConfigurer"> <property name="properties" ref="configProperties"> </property></bean>
2.app.properties文件中的内容是:
createNew=111
3.在Java的类中读取的方式是:
@Value("#{configProperties['createNew']}")public String aaa;
其中configProperties是application.xml中注入的bean。createNew是properties文件中的键值。
注意:按照上面的配置部署到测试环境之后,服务器可以正常使用,但是在日志里 会30秒刷新一次报错。报错信息是:
WARNING: Could not lock User prefs. Unix error code 2.
Mar 29, 2016 10:37:23 AM java.util.prefs.FileSystemPreferences syncWorld
WARNING: Couldn’t flush user prefs: java.util.prefs.BackingStoreException: Couldn’t get file lock.
更改为: application.xml中改为:
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:app.properties</value> </list> </property></bean>
或则在application.xml中配置为:
<context:property-placeholder location="classpath:app.properties" />
在Java的类中读取的方式是:
@Value("${createNew}")private String createNew;
4.app.properties文件部署带测试生产环境的问题
在实际的开发过程中我们会涉及到开发环境与测试环境所需要的同一个字段的不同数据,这个时候我们就会用到.properties文件。
如上图所示。开发环境需要使用dev中的配置文件,测试环境需要用到test中的配置文件。
第一步:我们需要做的是在pom.xml文件中进行配置。
<profiles><profile><id>dev</id><activation> <activeByDefault>true</activeByDefault></activation><properties> <configuration.directory>dev</configuration.directory> <build.packagingExcludes></build.packagingExcludes></properties> </profile><profile><id>test</id><properties> <configuration.directory>test</configuration.directory></properties></profile></profiles>
第二步:在打war包的时候需要把conf配置文件中的内容一起打包。需要在pom.xml中配置。
<build> <finalName>maven-springmvc</finalName> <resources> <!--表示把java目录下的有关xml文件,properties文件编译/打包的时候放在resource目录下--> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> </resource> <!--以下是打包的conf中配置文件内容。--> <resource> <directory>${basedir}/conf/${configuration.directory}</directory> <targetPath>${project.build.directory}/classes</targetPath> <filtering>true</filtering> </resource></resources></build>
- 注解式读取properties文件中的内容
- spring注解的方式读取properties文件中的内容
- 读取properties文件中的内容
- springMvc 如何通过注解@Value 读取配置文件中的内容(.properties)
- 读取properties文件内容
- properties文件读取内容
- 通过@Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- @Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- 通过@Value注解读取.properties配置内容
- java读取properties文件内容
- 读取properties文件中内容
- Java读取properties文件内容
- Java读取properties文件内容
- *args和**kwargs
- webpack项目流程学习(二)
- 漏洞利用查询工具sandi
- WIN10 + VS2015 + WDK10 + SDK10 + VM虚拟机驱动开发调试环境搭建
- CTX学长的快速幂(快速幂运用)
- 注解式读取properties文件中的内容
- mysql和oracle函数比较
- js点击按钮删除一行并传递参数小结1
- 论道数字经济、共享经济 第七届中国信息技术服务产业年会都说了啥?
- RecyclerView SpanSizeLookup
- PAT1012
- SharePoint Online 创建列表库
- 数据结构与算法分析(Java语言描述)(16)—— 二叉搜索树基础、节点插入、查找
- new 、delete运算符 、operator new 、operator delete 、malloc 、 free 函数