欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot2 application.properties方式加载配置文件

程序员文章站 2022-04-28 21:54:41
application.properties ......

SpringBoot2 application.properties方式加载配置文件

application.properties

 

jdbc.driverclassname=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou
jdbc.username=root
jdbc.password=

  

jdbcproperties.java
package cn.itcast.config;

import lombok.data;
import org.springframework.boot.context.properties.configurationproperties;

//注解配置文件属性
@configurationproperties(prefix = "jdbc")
//get和set方法
@data
public class jdbcproperties {
    private string url;
    private string driverclassname;
    private string username;
    private string password;
}

  

jdbcconfig.java
package cn.itcast.config;

import com.alibaba.druid.pool.druiddatasource;
import org.springframework.boot.context.properties.enableconfigurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

import javax.sql.datasource;

//配置文件注解
@configuration
@enableconfigurationproperties(jdbcproperties.class)
public class jdbcconfig {
    @bean
    public datasource datasource(jdbcproperties properties){
        druiddatasource druiddatasource = new druiddatasource();
        druiddatasource.setdriverclassname(properties.getdriverclassname());
        druiddatasource.seturl(properties.geturl());
        druiddatasource.setusername(properties.getusername());
        druiddatasource.setpassword(properties.getpassword());
        return druiddatasource;
    }
}

  

 SpringBoot2 application.properties方式加载配置文件