Websphere中获取项目下.properties路径

Posted 刀刀(2把刀)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Websphere中获取项目下.properties路径相关的知识,希望对你有一定的参考价值。

一:如果容器为Websphere,那下面为红色的地方不能加"/",如果为tomcat,则加上"/",

 

Java代码  技术分享
  1. String  path = this.class.getResource("").getPath()+"config.properties";  
  2.   
  3. Properties properties= new Properties();  
  4.   
  5. properties.load(new FileInputStream(new File(path )));  

 

如果你的config.properties在某个包下面,则把包同时带上,如:config.properties在com.df.util包下,则为:

Java代码  技术分享
  1. String  path = this.class.getResource("").getPath()+"com/df/util/config.properties";  
  2.   
  3. Properties properties= new Properties();  
  4.   
  5. properties.load(new FileInputStream(new File(path )));  

 

二:如果你的项目中用到了spring,那么也可这样获取,

Java代码  技术分享
  1. import org.springframework.core.io.Resource;  
  2.   
  3. import org.springframework.core.io.ClassPathResource;  
  4.   
  5. Resource resource = new ClassPathResource("config.properties");  //直接读取src下的,位于class文件之下
  6.   
  7. Properties  properties= new Properties();  
  8.   
  9. InputStream in = resource.getInputStream();  
  10.   
  11. properties.load(in);  

  

 

 

 

以上是关于Websphere中获取项目下.properties路径的主要内容,如果未能解决你的问题,请参考以下文章

IBM websphere MQ 消息发送与获取

Websphere查看修改节点端口

本地java类访问websphere的JNDI

向properties文件中写入信息(针对获取properties文件失败的总结)

log4j2查找websphere环境变量

websphere编译jsp生成的java类放在哪个目录