`

Spring调用配置文件Properties方法

 
阅读更多

配置文件位于classpath下

使用spring的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类加载Properties配置文件,通过源码可以知道,默认加载的是classpath下的文件,配置如下:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
     <value>classpath:config/init.properties</value>
  </property>
 </bean>

 

如果有多个配置文件加载,则:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
     <value>classpath:config/init.properties</value>
    <value>classpath:config/init.properties</value>
   </list>
  </property>
 </bean>

 

这样spring就能够加载properties文件了。

配置文件位于外部目录

  但是对于外部目录的配置文件,使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也是可以加载的,不过要修改他的路径配置方式,如下:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
     <value>file:${user.dir}/config/init.properties</value>
     <value>file:${user.dir}/config/init2.properties</value>
   </list>
  </property>
 </bean>

 

这样就可以成功加载外部目录的配置文件了,${user.dir}是系统变量,指用户当前目录所在。

代码中加载配置文件

  

private static final Properties sysConfig = new Properties();

    static {
        try {
            InputStream iStream = new FileInputStream(new File("config", "shellConfig.properties"));
            sysConfig.load(iStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static String getPropertyValue(String key){
        return sysConfig.getProperty(key);
    }

 

分享到:
评论

相关推荐

    SpringBoot入门(三) 之springboot的配置配置文件以及yaml的使用

    springboot使用Application.properties和Application.yml这两个作为全局配置文件,properties是一种常见的配置文件的形式 配置文件的作用就是来配置某些信息,修改某些默认值,在我们快速创建的springboot项目中就是...

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    允许动态方法调用 struts.freemarker.manager.classname The org.apache.struts2.views.freemarker.FreemarkerManager implementation class org.apache.struts2.views.freemarker.FreemarkerManager接口名 ...

    从零开始学Spring Boot

    1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@SpringBootApplication注解 1.36 Spring Boot 监控和管理生产环境 1.37 ...

    spring.doc

    2.2 Spring配置文件 7 2.3 Spring API 8 3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认...

    Spring.net框架

    为了消除MainApp对其它组件的依赖性,我们引入工厂模式,并且根据配置文件指定的装配规程,利用.net提供的反射技术完成对象的组装工作。 本部分代码仅仅提供一种功能演示,如果实际应用仍需进一步完善(建议使用一些...

    tool-link-properties:读取项目配置文件

    将配置文件log4j2.xml和spring-logging.xml从项目目录转移至tool-logging的包目录。 更改“自定义变量”使用“远程配置读取”方式的规则,["dev","test","real"]远程读取,其他则配置文件读取。 更改“全局变量”jsp...

    Spring MVC 入门实例

    上面, 我们在 web.xml 文件中告诉 ContextLoaderListener, 我们还有另外两个配置文件 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. applicationContext.xml: 1 2 &lt;!DOCTYPE beans PUBLIC "-//...

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form标签 ...

    Spring 2.0 开发参考手册

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    Spring API

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    springboot学习思维笔记.xmind

    SpringBoot的配置文件 application.properties application.yml src/main/resources starter pom 官方starter pom spring-boot-starter spring-boot-starter-actuator spring-...

    spring-boot-邮件发送

    添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还可以加入Thymeleaf相关的依赖。 配置邮件:在application.properties或者application.yml中配置邮件发送的...

    spring chm文档

    13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    SpringBoot 学习笔记+完整教程

    Spring boot(后续用*表示) hello...* 导入 spring XML 配置文件 * 热部署 * 监控和管理生产环境 * starter 详解 * 依赖的版本 * 文件上传 * 集成 redis 缓存 * 之 spring cache * 集成 EHCache * 分布式 Session 共享

    BOS 技术整理

    rc/main/resource 提供 config.properties 外部属性文件、log4j.properties 日志配置文件 配置文件如下: applicationContext.xml  struts2 注解 类上 @ParentPackage 包继承谁? extends=”struts-default...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    6.1.4 Spring Boot 的配置文件 140 6.1.5 starter pom 141 6.1.6 使用xml 配置 143 6.2 外部配置 143 6.2.1 命令行参数配置 143 6.2.2 常规属性配置 144 6.2.3 类型安全的配置(基于properties) 145 6.3 日志配置 ...

    redis-distributed-lock:redis分布式锁工具包,提供纯Java方式调用,支持传统Spring工程, 为spring boot应用提供了starter,更方便快捷的调用

    redis-distributed-lock redis分布式锁工具包,提供纯Java方式调用,支持传统Spring工程, 为spring boot应用提供了starter,更方便快捷的调用。 项目结构 原生redis分布式锁...配置文件application.properties中引

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    6.1.4 Spring Boot 的配置文件 140 6.1.5 starter pom 141 6.1.6 使用xml 配置 143 6.2 外部配置 143 6.2.1 命令行参数配置 143 6.2.2 常规属性配置 144 6.2.3 类型安全的配置(基于properties) 145 6.3 日志配置 ...

Global site tag (gtag.js) - Google Analytics