栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在非托管@Bean上使用@ConfigurationProperties

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在非托管@Bean上使用@ConfigurationProperties

由于这

DataSource
是类的私有内容,并且包含类的内容 可以
在Spring上下文中,因此您可以拥有一个
@ConfigurationProperties

@ConfigurationProperties("com.foo.bar.datasource.hikari")public class HikariConfiguration { ... }

通过进行注册

@EnableConfigurationProperties
,可用于自动装配

@EnableConfigurationProperties(HikariConfiguration.class)@SpringBootApplicationpublic class Application { ... }

因此可以在包含类中自动接线

@Componentclass MyClass {   private final HikariConfiguration hikariConfiguration;     private DataSource springDatasource;   MyClass(final HikariConfiguration hikariConfiguration) {      this.hikariConfiguration = hikariConfiguration;   }   ...   private DataSource privateSingletonDataSource() {      if (Objects.isNull(this.springDatasource)) {         this.springDatasource = buildDataSource(this.hikariConfiguration);      }      return this.springDatasource;   }}

buildDataSource
手动 构造
DataSource
实例。
请记住,构建时需要注意同步
DataSource


最后的回应是您不能重复使用

DataSourceProperties
。您甚至无法扩展它来更改属性的前缀。在上下文中只能存在一个实例。
您可以做的最好的事情就是模仿Spring的工作。

com.non.exposed.datasource.hikari.url=testUrlcom.non.exposed.datasource.hikari.username=testUsernamecom.non.exposed.datasource.hikari.password=testPassword...

您可以定义一个新

@ConfigurationProperties

@ConfigurationProperties("com.non.exposed.datasource")public class NonExposedProperties {    private final Map<String, String> hikari = new HashMap<>(8);    public Map<String, String> getHikari() {        return hikari;    }}

然后,将此属性类自动连接到您的

@Configuration
/
@Component
类中。
遵循代码中的注释。

@Configurationpublic class CustomConfiguration {    private final NonExposedProperties nonExposedProperties;    private DataSource dataSource;    CustomConfiguration(final NonExposedProperties nonExposedProperties) {        this.nonExposedProperties= nonExposedProperties;    }    public DataSource dataSource() {        if (Objects.isNull(dataSource)) { // Create a standalone instance of DataSourceProperties final DataSourceProperties dataSourceProperties = new DataSourceProperties(); // Use the NonExposedProperties "hikari" Map as properties' source. It will be // { //    url      -> testUrl //    username -> testUsername //    password -> testPassword //    ... other properties // } final ConfigurationPropertySource source = new MapConfigurationPropertySource(nonExposedProperties.getHikari()); // Bind those properties to the DataSourceProperties instance final BindResult<DataSourceProperties> binded =         new Binder(source).bind(      ConfigurationPropertyName.EMPTY,      Bindable.ofInstance(dataSourceProperties)         ); // Retrieve the binded instance (it's not a new one, it's the same as before) dataSource = binded.get().initializeDataSourceBuilder().build();        }        // Return the constructed HikariDataSource        return dataSource;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号