问题:
最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,不需要在进行查找修改代码。只针对于Springboot 使用
开发环境:
SpringBoot 2.1.0.RELEASE
Maven 3.3.9
Jdk 1.8
Idea 2018.1.2
三种常用的配置方式:以读取数据源的配置属性为例
第一种 java通用的配置方式:利用@Value注解
第一步:用IDEA 工具快速构建一个springboot 的web project 目录结构如下
第二步:pom文件 添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.bearhunting</groupId> <artifactId>configdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!--采用的是阿里巴巴的druid数据源-->
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.8</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第三步:创建要读取得配置文件 jdbc.properties,并添加属性值。
# 配置驱动 jdbc.driverClassName = com.mysql.jdbc.Driver # 数据库连接得url jdbc.url = jdbc:mysql://localhost:3306/test # 连接数据库的用户名 jdbc.username = root # 链接数据库得密码 jdbc.password = root
第四步:获取属性值,注入属性值
先创建一个配置类
package cn.bearhunting.configdemo.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; /** * @ClassName: jdbcConfig * @discription: 数据源属性配置 * @author: lele * @create: 2018-11-28 15:34 */ @Configuration @PropertySource("classpath:jdbc.properties") public class jdbcConfig { @Value("${jdbc.driverClassName}") private String driverClassName; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Bean public DataSource getDataSource(){ DruidDataSource dataSource =new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
注解说明:
@Configration
用于定义声明配置类,等同于xml配置文件
@PropertySource
可以点进去看源码,里面有示例
@Value("${jdbc.driverClassName}")
读取配置文件的属性值
@Bean 配合@Configration使用,作用在方法上,相当于xml配置文件<bean><?bean>
第五步:测试类 DUG 测试结果
package cn.bearhunting.configdemo; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.sql.DataSource; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired private DataSource dataSource; @Test public void contextLoads() { System.out.println(dataSource); } }
获取数据成功
第二种方法:springboot 的配置方式
第三步,要把jdbc.properties 文件名改成 application.properties (application.properties是springboot的默认的配置文件,会自动读取)
第四步,创建一个JdbcProperties属性配置类如下
package cn.bearhunting.configdemo.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @ClassName: JdbcProperties * @discription: * @author: lele * @create: 2018-11-28 16:25 */ @ConfigurationProperties(prefix = "jdbc") @Data public class JdbcProperties { private String driverClassName; private String url; private String username; private String password; }
@ConfigrationProperties(prefix = "jdbc")
指定前缀,属性文件的属性名和配置类的属性名要完全一致;
@Data
了解一下lombok 此处不做说明,作用是不用写set get 方法
第五步 将JdbcConfig 类进行改写
package cn.bearhunting.configdemo.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; /** * @ClassName: jdbcConfig * @discription: 数据源属性配置 * @author: lele * @create: 2018-11-28 15:34 */ @Configuration @EnableConfigurationProperties(JdbcProperties.class) public class jdbcConfig { @Bean public DataSource getDataSource(JdbcProperties jdbcProperties){ DruidDataSource dataSource =new DruidDataSource(); dataSource.setDriverClassName(jdbcProperties.getDriverClassName()); dataSource.setUrl(jdbcProperties.getUrl()); dataSource.setUsername(jdbcProperties.getUsername()); dataSource.setPassword(jdbcProperties.getPassword()); return dataSource; } }
注解说明:
@EnableConfigurationProperties(JdbcProperties.class)
开启使用属性配置类 JdbcProperties
最后获取数据成功
第三种,最简洁的一种
不需要属性配置类 ,直接注入
package cn.bearhunting.configdemo.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * @ClassName: jdbcConfig * @discription: 数据源属性配置 * @author: lele * @create: 2018-11-28 15:34 */ @Configuration public class jdbcConfig { @Bean @ConfigurationProperties(prefix = "jdbc") public DataSource getDataSource(){ return new DruidDataSource(); } }