java程序运行时读取property文件

版权声明: https://blog.csdn.net/qq_36004521/article/details/81481888

SpringBoot项目

1.编写properties文件读取工具类

package com.hontye.parameter.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Date: 2018/3/13 13:06
 * User: pc
 * Description:自定义properties文件读取工具类
 */

public class PropertyFileUtils implements ServletContextListener {
    private static Properties prop = new Properties();
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        InputStream inputStream;
        try {
            inputStream = getClass().getResourceAsStream("/application.properties");
            if(inputStream != null){
                prop.load(inputStream);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {


    }

    public static String get(String params){
        return prop.getProperty(params);
    }
}

2.配置监听器

package com.hontye.parameter;

import com.hontye.Database.NFDFlightDataTaskListener;
import com.hontye.parameter.util.PropertyFileUtils;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* 监听器配置
* */

@Configuration
public class ListenerConfig {
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new PropertyFileUtils());
        return servletListenerRegistrationBean;
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean1(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new NFDFlightDataTaskListener());
        return servletListenerRegistrationBean;
    }
}

3.application.properties文件内容如下:

server.port=8081
	 
#MYSQL数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.51:3306/qqgg
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jackson.serialization.indent_output=true


# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.1.101
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

# influxdb地址
influxdb.openurl=http://192.168.1.101:8086
# influxdb用户名
influxdb.username=root
# influxdb连接密码
influxdb.password=root
# influxdb数据库名
influxdb.database=PARAMTER_DB


jolokia.config.debug=true  
endpoints.jolokia.enabled=true  
#endpoints.jolokia.sensitive=true  
endpoints.jolokia.path=/jolokia  
endpoints: enabled: true jmx: enabled: true jolokia: enabled: true management: security: enabled: false  

#设置该类日志级别为debug,否则该类不显示debug日志
logging.level.com.hontye.parameter.controller.RedisSetParameter = debug
#没有logback-spring.xml配置文件时,会在此项目的根目录(workspace中)生成MyLog.log
logging.file=MyLog.log
#文件日志存储目录,只有创建了logback-spring.xml,并且fileNamePatte……
logging.path=hydrogenLogs/logs
#logback-spring.xml文件中,用<file>${LOG_PATH}/${LOG_FILE}</file>获取以上路径和文件名配置

#数据库备份时间
backuptime=00:00:00
#数据库所在的主机地址
hostIp=192.168.1.51
#数据库bin目录
#windows版
binUrl=C:/MySQL/bin
#mac版
#binUrl=/usr/local/mysql-5.7.22-macos10.13-x86_64/bin
#备份保存目录
savePath=backups/hydrogen/
#要备份的数据库名
databaseName=qqgg

4.其他文件中通过get取值

/**
     * Java代码实现MySQL数据库导出
     *
     * @author GaoHuanjie
     * @param hostIP MySQL数据库所在服务器地址IP
     * @param userName 进入数据库所需要的用户名
     * @param password 进入数据库所需要的密码
     * @param savePath 数据库导出文件保存路径
     * @param databaseName 要导出的数据库名
     * @return 返回true表示导出成功,否则返回false。
     */
    private static String hostIP = PropertyFileUtils.get("hostIp");
    private static String userName = PropertyFileUtils.get("spring.datasource.username");
    private static String password = PropertyFileUtils.get("spring.datasource.password");
    private static String savePath = PropertyFileUtils.get("savePath");
    private static String databaseName = PropertyFileUtils.get("databaseName");
    private static String binUrl = PropertyFileUtils.get("binUrl");

猜你喜欢

转载自blog.csdn.net/qq_36004521/article/details/81481888