优化日志数据库配置类
This commit is contained in:
+11
-1
@@ -1,8 +1,12 @@
|
||||
package com.central.log.config;
|
||||
|
||||
import com.central.log.properties.AuditLogProperties;
|
||||
import com.central.log.properties.LogDbProperties;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 日志自动配置
|
||||
@@ -12,5 +16,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
*/
|
||||
@EnableConfigurationProperties({TraceProperties.class, AuditLogProperties.class})
|
||||
public class LogAutoConfigure {
|
||||
|
||||
/**
|
||||
* 日志数据库配置
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(HikariConfig.class)
|
||||
@EnableConfigurationProperties(LogDbProperties.class)
|
||||
public static class LogDbAutoConfigure {}
|
||||
}
|
||||
|
||||
-5
@@ -27,9 +27,4 @@ public class AuditLogProperties {
|
||||
* 日志记录类型(logger/redis/db/es)
|
||||
*/
|
||||
private String logType;
|
||||
/**
|
||||
* 数据源配置
|
||||
* logType=db时生效(非必须),如果不配置则使用当前数据源
|
||||
*/
|
||||
private LogDbProperties datasource = new LogDbProperties();
|
||||
}
|
||||
|
||||
+3
@@ -3,9 +3,11 @@ package com.central.log.properties;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 日志数据源配置
|
||||
* logType=db时生效(非必须),如果不配置则使用当前数据源
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/2/8
|
||||
@@ -15,5 +17,6 @@ import lombok.Setter;
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.audit-log.datasource")
|
||||
public class LogDbProperties extends HikariConfig {
|
||||
}
|
||||
|
||||
+5
-3
@@ -2,10 +2,12 @@ package com.central.log.service.impl;
|
||||
|
||||
import com.central.log.model.Audit;
|
||||
import com.central.log.properties.AuditLogProperties;
|
||||
import com.central.log.properties.LogDbProperties;
|
||||
import com.central.log.service.IAuditService;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -33,10 +35,10 @@ public class DbAuditServiceImpl implements IAuditService {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public DbAuditServiceImpl(AuditLogProperties auditLogProperties, DataSource dataSource) {
|
||||
public DbAuditServiceImpl(@Autowired(required = false) LogDbProperties logDbProperties, DataSource dataSource) {
|
||||
//优先使用配置的日志数据源,否则使用默认的数据源
|
||||
if (StringUtils.isNotEmpty(auditLogProperties.getDatasource().getJdbcUrl())) {
|
||||
dataSource = new HikariDataSource(auditLogProperties.getDatasource());
|
||||
if (logDbProperties != null && StringUtils.isNotEmpty(logDbProperties.getJdbcUrl())) {
|
||||
dataSource = new HikariDataSource(logDbProperties);
|
||||
}
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user