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 ...
随机推荐
- 吴裕雄--天生自然ORACLE数据库学习笔记:数据表对象
create table students( stuno ) not null, --学号 stuname ), --姓名 sex ), --性别 age int, --年龄 departno ) n ...
- C++ 类型、类型转换
C++ 数据类型 基本内置类型 字面值常量和字面值类型 类类型 隐式的类类型转换 聚合类 字面值常量类 constexpr 构造函数 类的静态成员 使用编程语言进行编程时,需要用到各种变量来存储各种信 ...
- C# Stream篇(三) -- TextWriter 和 StreamWriter---转载
C# Stream篇(三) -- TextWriter 和 StreamWriter TextWriter 和 StreamWriter 目录: 为何介绍TextWriter? TextWriter的 ...
- 【转】CGI 和 FastCGI 协议的运行原理
介绍 深入CGI协议 CGI的运行原理 CGI协议的缺陷 深入FastCGI协议 FastCGI协议运行原理 为什么是 FastCGI 而非 CGI 协议 CGI 与 FastCGI 架构 再看 Fa ...
- c++读取注册表的实例
// CRegisterTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #in ...
- canvas画扇形、饼图
画扇形的方法 方法一:起始角度是0,那么第一条线就是line(r,0),通过旋转扇形的角度,第二条线就是line(r,0) //圆弧 ctx.save(); ctx.translate(100, 10 ...
- 设计模式课程 设计模式精讲 11-2 装饰者模式coding
1 代码演练 1.1 代码演练1(未使用装饰者模式) 1.2 代码演练2(使用装饰者模式) 1 代码演练 1.1 代码演练1(未使用装饰者模式) 需求: 大妈下班卖煎饼,加一个鸡蛋加一元,一个火腿两元 ...
- linux下的文件操作
彻底删除文件 rm -rf + [文件目录 可相对可绝对] 是彻底删除而且linux无回收站 创建文件 touch + [文件名] 创建文件夹 mkdir + [文件夹名] 文件提权:chmod 77 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:除了屏幕阅读器外,其他设备上隐藏元素
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- JavaScript引用类型与对象
1.引用类型 引用类型的值(对象)是引用类型的一个实例.引用类型有时候也被称为对象定义,因为它们描述的是一类对象所具有的属性和方法. 对象是某个特定引用类型的实例.新对象是使用new操作符后跟一个构造 ...