SpringBoot2 application.properties方式加载配置文件
程序员文章站
2022-04-28 21:54:41
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;
}
}
上一篇: 太多吃不完