目的:能够使用@ConfigurationProperties实现Spring Boot配置文件配置项读取(对象中)和应用
分析:
需求:将配置文件中的配置项读取到一个对象中;
实现:可以使用Spring Boot提供的注解@ConfigurationProperties,该注解可以将Spring Boot的配置文件(默认必须application.properties或application.yml)中的配置项读取到一个对象中
实现步骤:
1.加入注解依赖(不加入依赖@ConfigurationProperties会报错)
2.创建配置项类(jdbcProperties类),在改类名上添加注解@ConfigurationProperties,就可以把配置文件读取到此类上.
3.将jdbc.properties修改名称为application.properties
4.将jdbcProperites对象注入到JdbcConfig
5.测试
===========================
1.加入注解依赖(不加入依赖@ConfigurationProperties会报错)
<?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> <!--SpringBoot父工程--> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.3.4.RELEASE</version> </parent> <groupId>org.cc8w</groupId> <artifactId>javaboot_test</artifactId> <version>1.0-SNAPSHOT</version> <!--java编译版本,父工程已经指定了这样的变量--> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--SpringBoot Web启动器(不用写版本跟随父版本)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--在配置注解@ConfigurationProperties加的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency> </dependencies> </project>
2.创建配置项类(jdbcProperties类),在改类名上添加注解@ConfigurationProperties,就可以把配置文件读取到此类上.
package com.cc8w.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @ConfigurationProperties从application配置文件中读取配置项 * prefix 表示配置项的前缀,配置了就不用写前缀了 * 配置项类中的类变量必须要和前缀之后的配置项名称一致 */ @ConfigurationProperties(prefix = "jdbc") public class JdbcProperties { private String driverClassName; private String url; private String username; private String password; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
3.将jdbc.properties修改名称为application.properties (略)
4.将jdbcProperites对象注入到JdbcConfig
package com.cc8w.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; @Configuration //@PropertySource("classpath:jdbc.properties") @EnableConfigurationProperties(JdbcProperties.class) 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 dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } */ @Bean public DataSource dataSource(JdbcProperties jdbcProperties){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(jdbcProperties.getDriverClassName()); dataSource.setUrl(jdbcProperties.getUrl()); dataSource.setUsername(jdbcProperties.getUsername()); dataSource.setPassword(jdbcProperties.getPassword()); return dataSource; } }
5.目录结构和测试