Get value from properties file outside a jar using Spring
来源:互联网 发布:spss问卷数据录入教程 编辑:程序博客网 时间:2024/05/29 16:26
I have a jar file which need to dynamically load external properties file and read values from this properties file. Those values are needed in the java program. With Spring, it's very easy to do that.
2. config your applicationContext.xml.
4. Last thing, our main class: PortletCreatorMain
1. create a config.properties
portlet.groupId=com.rujuanportlet.artifactId=hello-world
2. config your applicationContext.xml.
The bean PropertyPlaceholderConfigurer. It provides the functionality which we can use the properties file inside jar or use external properties file.
The second thing is how we get those values in the property. Here I wrote a POJO class, and config those key as as property in the POJO class.
<?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:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>classpath:defaultconfig.properties</value><value>file:${target_env}</value></list></property></bean><bean id="createPortlet" class="com.rujuan.Portlet"><property name="groupId" value="${portlet.groupId}" /><property name="artifactId" value="${portlet.artifactId}" /></bean> </beans>
public class Portlet {private String groupId;private String artifactId;public String getGroupId() {return groupId;}public void setGroupId(String groupId) {this.groupId = groupId;}public String getArtifactId() {return artifactId;}public void setArtifactId(String artifactId) {this.artifactId = artifactId;}}
4. Last thing, our main class: PortletCreatorMain
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class PortletCreatorMain {/** * @param args */public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Portlet portlet = context.getBean("createPortlet", Portlet.class);System.out.println(portlet.getArtifactId());}}In this case, you can get the value from Portlet properties.
- Get value from properties file outside a jar using Spring
- get file's name from a folder using Java
- Get a file's properties
- poi.jar处理Excel错误Cannot get a text value from a numeric cell
- Get a hash value using XS
- Read file from inside a JAR file
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- Read properties file in a jar
- Get the Mime Type from a File
- Get the Mime Type from a File
- Cannot get a text value from a numeric cell
- Cannot get a formula value from a text cell
- Cannot get a text value from a numeric cell “Poi”
- Obtain Key from a Hash of Arrays using array value
- Using SOM to get a feature in a file
- wprintf, fork, fwide, get FILE* from a file descriptor
- jar - user of jar command to list/extract file from a jar file
- 关于MyEclipse较检异常 Invalid result location value/parameter 及Cannot return from outside a function or
- 语言的界限就是一个人世界的界限
- using HTML Helper for radiobutton!
- 和游戏开发爱好者浅谈“自学”
- 买N多书≠学会C++
- 凉水洗脸真的好处多吗?
- Get value from properties file outside a jar using Spring
- 曾经我以为自己很努力,其实我连努力的边都没看到。
- SQL Server 知识总结
- WS(webService) 的简单应用、实战案例
- Ubuntu下Android SDK Manager无法更新的解决
- 信号 中断系统调用 递送
- iptable
- 358. Practise makes perfect. 熟能生巧
- 动态规划法中的最长公共子序列