SpringBoot 读取Properties,不用spring boot 中的jdbctemplate。

      通过spring boot 启动加载自己编写的properties数据库配置文件,从而进行系统的参数读取及配置信息。不用spring boot 中的jdbctemplate。

 

步骤:

  一、编写properties文件, 放置到resource目录下(我的命名为:dbConfig.properties)

url=jdbc:mysql://127.0.0.1/jxsystem?serverTimezone=UTC&useUnicode=true&charaterEncoding=utf-8&useSSL=false
name=com.mysql.cj.jdbc.Driver
user=root
password=mysql

  二、编写PropertyUtil.java读取配置类

public class PropertyUtil {
    private static Map<String, String> propertiesMap = new HashMap<>();
    // Default as in PropertyPlaceholderConfigurer


    public static void processProperties( Properties props) throws BeansException {

        propertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();

            try {
                //PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
                propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }catch (java.lang.Exception e){
                e.printStackTrace();
            };
        }
        System.out.println(propertiesMap);
    }
    public static void loadAllProperties(){
        try {

            Properties properties = PropertiesLoaderUtils.loadAllProperties("dbConfig.properties");
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String name) {
        //如果系统启动加载时,没有读到信息,则进行一次补读
        if(propertiesMap.size()==0){
            loadAllProperties();
        }
        return propertiesMap.get(name).toString();
    }
}

以上方法基本上是可以读取到properties文件中的信息了,如果是想在spring boot 启动中时读取,则需要以下一个监听器并且需要通过修改Application.java文件

三、编写ApplicationStartUpListener.java监听器类

public class ApplicationStartUpListener implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        PropertyUtil.loadAllProperties();
        System.out.println("ApplicationStartUpListener EXEC");
    }
}

四、修改Application.java文件

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners(new ApplicationStartUpListener());
        application.run(args);
 
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

以上仅共学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值