浏览代码

优化注入

欧阳劲驰 2 周之前
父节点
当前提交
4eaef18ecc

+ 7 - 12
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/DataSourceConfig.java

@@ -26,20 +26,14 @@ import static com.shkpr.service.customgateway.core.constants.DataSourceNames.PRI
 @EnableConfigurationProperties(DataSourceProperties.class)
 @PropertySource(value = "file:${global.sql-config-path}", ignoreResourceNotFound = true, encoding = "UTF-8", factory = YamlPropertySourceFactory.class)
 public class DataSourceConfig {
-    final
-    DataSourceProperties dataSourceProperties;
-
-    public DataSourceConfig(DataSourceProperties dataSourceProperties) {
-        this.dataSourceProperties = dataSourceProperties;
-    }
-
     /**
+     * @param properties 属性
      * @return 主要数据源
      */
     @Bean(name = PRIMARY + "DataSource")
-    public DataSource primaryDataSource() {
+    public DataSource primaryDataSource(DataSourceProperties properties) {
         //获取配置
-        DataSourceProperties.DataSource source = dataSourceProperties.getMulti().get(PRIMARY);
+        DataSourceProperties.DataSource source = properties.getMulti().get(PRIMARY);
         //构建数据源
         HikariDataSource dataSource = DataSourceBuilder.create()
                 .type(HikariDataSource.class)
@@ -48,18 +42,19 @@ public class DataSourceConfig {
                 .password(source.getPassword())
                 .build();
 
-        configHikari(dataSourceProperties.getHikari(), dataSource);
+        configHikari(properties.getHikari(), dataSource);
 
         return dataSource;
     }
 
     /**
+     * @param properties 属性
      * @return 嵌入数据源
      */
     @Bean(name = EMBEDDED + "DataSource")
-    public DataSource embeddedDataSource() {
+    public DataSource embeddedDataSource(DataSourceProperties properties) {
         //获取配置
-        DataSourceProperties.DataSource source = dataSourceProperties.getMulti().get(EMBEDDED);
+        DataSourceProperties.DataSource source = properties.getMulti().get(EMBEDDED);
         //构建数据源
         return DataSourceBuilder.create()
                 .type(JdbcDataSource.class)

+ 13 - 30
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/InfluxDbConfig.java

@@ -25,48 +25,31 @@ import java.util.stream.Collectors;
 @EnableConfigurationProperties(InfluxDbProperties.class)
 @PropertySource(value = "file:${global.sql-config-path}", ignoreResourceNotFound = true, encoding = "utf-8", factory = YamlPropertySourceFactory.class)
 public class InfluxDbConfig {
-    final
-    OkHttpClient.Builder builder;
-    final
-    InfluxDbProperties influxDbProperties;
-
-    public InfluxDbConfig(ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder
-            , ObjectProvider<OkHttpClient.Builder> deprecatedBuilder, InfluxDbProperties influxDbProperties) {
-        this.builder = determineBuilder(builder.getIfAvailable(),
-                deprecatedBuilder.getIfAvailable());
-        this.influxDbProperties = influxDbProperties;
-    }
-
-    /**
-     * 确认构建器
-     *
-     * @param builder           默认构建器
-     * @param deprecatedBuilder 弃用构建器
-     * @return 可使用的构建器
-     */
-    private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder
-            , OkHttpClient.Builder deprecatedBuilder) {
-        return builder != null ? builder.get()
-                : deprecatedBuilder != null ? deprecatedBuilder
-                : new OkHttpClient.Builder();
-    }
-
     /**
      * influxDb注册器
      *
+     * @param properties 属性
+     * @param providers  构建器提供
      * @return influxDb注册器
      */
     @Bean
-    public InfluxDBClients influxDBClients() {
-        this.builder.readTimeout(influxDbProperties.getReadTimeout());
+    public InfluxDBClients influxDBClients(InfluxDbProperties properties, ObjectProvider<InfluxDbOkHttpClientBuilderProvider> providers) {
+        //构建器
+        OkHttpClient.Builder builder = determineBuilder(providers.getIfAvailable());
+        builder.readTimeout(properties.getReadTimeout());
         //客户端集合
-        List<InfluxDbClient> clients = influxDbProperties.getClients().stream()
+        List<InfluxDbClient> clients = properties.getClients().stream()
                 .map(client -> new InfluxDbClient(client.getUrl(), client.getUser(), client.getPassword()
-                        , client.getDatabase(), this.builder))
+                        , client.getDatabase(), builder))
                 .collect(Collectors.toList());
         //注册
         return InfluxDBClients.builder()
                 .addClients(clients)
                 .build();
     }
+
+    private OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder) {
+        if (builder != null) return builder.get();
+        return new OkHttpClient.Builder();
+    }
 }

+ 4 - 10
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/JdbcRepositoriesConfig.java

@@ -20,20 +20,16 @@ import static com.shkpr.service.customgateway.core.constants.DataSourceNames.EMB
  * @since 1.0.0
  */
 @Configuration
-@EnableJdbcRepositories(
-        jdbcOperationsRef = EMBEDDED + "JdbcOperations",
+@EnableJdbcRepositories(jdbcOperationsRef = EMBEDDED + "JdbcOperations",
         transactionManagerRef = EMBEDDED + "TransactionManager",
-        basePackages = "com.shkpr.service.customgateway.core.repository." + EMBEDDED
-)
+        basePackages = "com.shkpr.service.customgateway.core.repository." + EMBEDDED)
 public class JdbcRepositoriesConfig {
     /**
      * @param dataSource 数据源
      * @return jdbc操作器
      */
     @Bean(EMBEDDED + "JdbcOperations")
-    public NamedParameterJdbcOperations embeddedJdbcOperations(
-            @Qualifier(EMBEDDED + "DataSource") DataSource dataSource
-    ) {
+    public NamedParameterJdbcOperations embeddedJdbcOperations(@Qualifier(EMBEDDED + "DataSource") DataSource dataSource) {
         return new NamedParameterJdbcTemplate(dataSource);
     }
 
@@ -42,9 +38,7 @@ public class JdbcRepositoriesConfig {
      * @return 事务管理器
      */
     @Bean(EMBEDDED + "TransactionManager")
-    public PlatformTransactionManager embeddedTransactionManager(
-            @Qualifier(EMBEDDED + "DataSource") DataSource dataSource
-    ) {
+    public PlatformTransactionManager embeddedTransactionManager(@Qualifier(EMBEDDED + "DataSource") DataSource dataSource) {
         return new DataSourceTransactionManager(dataSource);
     }
 }

+ 4 - 12
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/config/MybatisConfig.java

@@ -25,28 +25,20 @@ import static com.shkpr.service.customgateway.core.constants.DataSourceNames.PRI
  */
 @Configuration
 @EnableConfigurationProperties(MybatisProperties.class)
-@MapperScan(
-        sqlSessionTemplateRef = PRIMARY + "SqlSessionTemplate",
-        basePackages = "com.shkpr.service.customgateway.**.mapper." + PRIMARY
-)
+@MapperScan(sqlSessionTemplateRef = PRIMARY + "SqlSessionTemplate",
+        basePackages = "com.shkpr.service.customgateway.**.mapper." + PRIMARY)
 public class MybatisConfig {
 
     private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
 
-    final
-    MybatisProperties properties;
-
-    public MybatisConfig(MybatisProperties properties) {
-        this.properties = properties;
-    }
-
     /**
      * @param dataSource 数据源
      * @return SQL会话工厂
      * @throws Exception NotInitializedException
      */
     @Bean(PRIMARY + "SqlSessionFactory")
-    public SqlSessionFactory mainSqlSessionFactoryBean(@Qualifier(PRIMARY + "DataSource") DataSource dataSource) throws Exception {
+    public SqlSessionFactory mainSqlSessionFactoryBean(MybatisProperties properties
+            , @Qualifier(PRIMARY + "DataSource") DataSource dataSource) throws Exception {
         SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
         sessionFactoryBean.setDataSource(dataSource);
         sessionFactoryBean.setMapperLocations(resourceResolver.getResources("classpath*:mapper/*.xml"));