SpringBoot-属性文件properties形式
SpringBoot-属性文件properties形式
上述使用JavaBean的配置可以实现数据源的配置,但是如果配置文件中的内容需要被多次调用就没那么方便了,所以我们学习新的方法,将Properties文件中的公用属性映射到类中的属性
SpringBoot中使用Java方式配置步骤如下:
创建一个名为application.properties <必须>的文件
将jdbc.properties中的内容放入application.properties 中
创建一个Java类例如JDBCProperties,加入@ConfigurationProperties注解(此注解拥有一个prefix属性,可以指定前缀)
在此类中声明的属性必须和Properties文件中的属性名一致(如果有前缀则为前缀点后的属性名)
此类需要提供getter、setter方法用于注入属性,可以使用lombok
在其他类中就可以直接使用此类中的属性了
packagecn.rayfoo.config;
importlombok.Data;
importorg.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author 张瑞丰
* @description
* @date 2019/11/4
*/
@ConfigurationProperties(prefix="jdbc")
@Data
publicclassJDBCProperties{
privateStringurl;
privateStringdriver;
privateStringusername;
privateStringpassword;
}
优化Java配置代码
在上一篇文章中,我们介绍了JavaBean配置的方法,这里我们优化一下其代码
去掉@PropertiesSource注解
新增@EnableConfigurationProperties注解,在其属性中指定JDBCProperties的字节码
去掉此类中的四个属性
使用@Autowired注解注入JDBCProperties类
在数据源中使用jdbcProperties类实例的getter方法
packagecn.rayfoo.config;
importcom.alibaba.druid.pool.DruidDataSource;
importlombok.Data;
importlombok.ToString;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.PropertySource;
importjavax.sql.DataSource;
/**
* @author 张瑞丰
* @description
* @date 2019/11/4
*/
@Configuration
@EnableConfigurationProperties(JDBCProperties.class)
publicclassJDBCConfig{
@Autowired
privateJDBCPropertiesjdbcProperties;
@Bean
publicDataSourcedataSource(){
DruidDataSourcedruidDataSource=newDruidDataSource();
druidDataSource.setUsername(jdbcProperties.getUsername());
druidDataSource.setPassword(jdbcProperties.getPassword());
druidDataSource.setUrl(jdbcProperties.getUrl());
druidDataSource.setDriverClassName(jdbcProperties.getDriver());
returndruidDataSource;
}
}
如果不适用注入的方式还可以将公用类JDBCProperties作为dataSource方法的参数,SpringBoot会自动将bean对象注入该参数
packagecn.rayfoo.config;
importcom.alibaba.druid.pool.DruidDataSource;
importlombok.Data;
importlombok.ToString;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.PropertySource;
importjavax.sql.DataSource;
/**
* @author 张瑞丰
* @description
* @date 2019/11/4
*/
@Configuration
@EnableConfigurationProperties(JDBCProperties.class)
publicclassJDBCConfig{
@Bean
publicDataSourcedataSource(JDBCPropertiesjdbcProperties){
DruidDataSourcedruidDataSource=newDruidDataSource();
druidDataSource.setUsername(jdbcProperties.getUsername());
druidDataSource.setPassword(jdbcProperties.getPassword());
druidDataSource.setUrl(jdbcProperties.getUrl());
druidDataSource.setDriverClassName(jdbcProperties.getDriver());
returndruidDataSource;
}
}
SpringBoot-属性文件properties形式的更多相关文章
- Spring的属性文件properties使用注意
Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...
- Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- Spring中使用属性文件properties的两种方式
实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...
- SpringBoot的配置属性文件*.properties值如何映射到类中使用
想要在JAVA Bean中读取配置文件中的内容有两种方式,可以进行获取到 第一种方式: 1.在默认的配置文件application.properties 中进行设置 Key-Value键值对 com. ...
- Java动态加载属性文件.properties
当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties prop ...
- 使用JAVA读写Properties属性文件
使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...
- SpringBoot @Value读取properties文件的属性
SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...
- (七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取
默认访问的属性文件为application.properties文件,可在启动项目参数中指定spring.config.location的参数: java -jar myproject.jar --s ...
- 章节六、3-读取Properties属性文件
一.如何读取Properties文件1.创建一个名为ReadingProperties的类 2.创建一个.propertise属性的文件,创建的方式参考“二”中步骤 3.写入如下代码 package ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:段落中超出屏幕部分不换行
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- 第2节 Scala中面向对象编程:7、继承的概念以及override和super关键字;8、isInstanceOf 和 asInstanceOf关键字
6.3. Scala面向对象编程之继承 6.3.1. Scala中继承(extends)的概念 Scala 中,让子类继承父类,与 Java 一样,也是使用 extends 关键字: 继承 ...
- vb.net自学完整版
https://m.book118.com/html/2016/1203/67671992.shtm
- 等级保护2.0-oracle
- Django(二十)分页:
一.知识点 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/pagination/ 查询出所有省级地区的信息,显示在页面上. AeroInfo ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-warning" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- zabbix开启对中文的支持--&&--中文乱码解决方法
zabbix不支持中文图 开启zabbix对中文的支持 原来zabbix默认把对中文的支持给关闭了,我们需要修改zabbix的php源文件. 修改站点根目录下include/locales.inc.p ...
- CSS去掉背景颜色
background-color:transparent;
- Vue 集成easyUI
原 Vue 集成easyUI https://blog.csdn.net/m0_37948170/article/details/84960320 参考vue官网用cli创建了Vue项目之后: n ...
- JS 自动返回每个月的天数
);//M代表月份,可以自己手动设置或者选择 date.setDate() var num = date.getDate(); console.log(num) //输出每个月的天数 var time ...