spring boot in @ConfigurationProperties use

  Project development, we sometimes want the profile information, and automatically reading package entity classes, like this, we use the code in which more convenient. We can use @ConfigurationProperties, the configuration information of entity class used to automatic packaging. Wherein the configuration information in a configuration file or the configuration center, for example Nacos.

Profile (center) of the following information:

liaowenhui.datasource:
  validationQuery: SELECT 1
  jdbcUrl:jdbc:mysql://192.168.xx.xx:3306/xxx?useUnicode=true&characterEncoding=UTF-8
  jdbcUserName: liaowenhui
  jdbcUserPassword: liaowenhui

Entity class defines a load profile information to DataSourceProperties.java

// configuration information from the information search prefix liaowenhui.datasource reads and automatically encapsulated entity class 
@ConfigurationProperties (prefix = "liaowenhui.datasource") public class DataSourceProperties {
Private String validationQuery; Private String the jdbcUrl; Private String jdbcUserName; Private String jdbcUserPassword; public String getValidationQuery () { return validationQuery; } public void setValidationQuery (String validationQuery) { the this .validationQuery = validationQuery; } public String getJdbcUrl () { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getJdbcUserName() { return jdbcUserName; } public void setJdbcUserName(String jdbcUserName) { this.jdbcUserName = jdbcUserName; } public String getJdbcUserPassword() { return jdbcUserPassword; } public void setJdbcUserPassword(String jdbcUserPassword) { this.jdbcUserPassword = jdbcUserPassword; } }

Use @EnableConfigurationProperties (xxx.class) need to explicitly specify which entity class used to load configuration information

@Configuration
@EnableConfigurationProperties({DataSourceProperties.class })
public class DataSourceConfig { private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class); @Bean(destroyMethod = "close") @RefreshScope public DataSource dataSourcePgProduct(DataSourceProperties dataSourceProperties) throws SQLException { logger.debug("数据库连接信息:{} ", dataSourceProperties.getJdbcUrl()); DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(dataSourceProperties.getJdbcUrl()); dataSource.setUsername(dataSourceProperties.getJdbcUserName()); dataSource.setPassword(dataSourceProperties.getJdbcUserPassword()); dataSource.setValidationQuery(dataSourceProperties.getValidationQuery());      ...... dataSource.init(); return dataSource; }

Description: @EnableConfigurationProperties equivalent to the use @ConfigurationProperties classes conducted an injection, but not necessarily @EnableConfigurationProperties notes to use, can be used directly @Autowired DataSourceProperties dataSourceProperties be injected, if not @EnableConfigurationProperties, then, in addition to the entity classes to add @ ConfigurationProperties plus @Component configuration information to the IOC injected into the container.

May refer to: About @EnableConfigurationProperties comment

Guess you like

Origin www.cnblogs.com/liaowenhui/p/11203296.html