optimize bean

This commit is contained in:
trang
2017-09-20 17:02:57 +08:00
parent 15f28c2595
commit 5493703a85
6 changed files with 28 additions and 15 deletions
+5 -2
View File
@@ -1,8 +1,11 @@
# TODO
## Add
## Feature
* ~~新增 ConfigFilter 配置,替换默认的 `connectionProperties` 方式~~
* 新增 `@EnableDynamicDruidDataSource`,自动配置动态数据源
* 新增 `Health Indicator`
* 新增 `Faliure Analyzer`
* 新增 `Endpoints`
## Bug
* ~~`spring.datasource.schema` 会执行两次~~
@@ -44,24 +44,23 @@ public class DruidAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(DruidAutoConfiguration.class);
@ConditionalOnProperty(prefix = DRUID_STAT_FILTER_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@Bean
@ConditionalOnProperty(prefix = DRUID_STAT_FILTER_PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
@ConfigurationProperties(DRUID_STAT_FILTER_PREFIX)
public StatFilter statFilter() {
log.debug("druid stat-filter init...");
return new StatFilter();
}
@ConditionalOnProperty(prefix = DRUID_WALL_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_WALL_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_WALL_CONFIG_PREFIX)
public WallConfig wallConfig() {
return new WallConfig();
}
@ConditionalOnProperty(prefix = DRUID_WALL_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_WALL_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_WALL_FILTER_PREFIX)
public WallFilter wallFilter(WallConfig wallConfig) {
log.debug("druid wall-filter init...");
@@ -70,47 +69,47 @@ public class DruidAutoConfiguration {
return filter;
}
@ConditionalOnProperty(prefix = DRUID_CONFIG_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_CONFIG_FILTER_PREFIX, name = "enabled", havingValue = "true")
public ConfigFilter configFilter() {
log.debug("druid config-filter init...");
return new ConfigFilter();
}
@ConditionalOnProperty(prefix = DRUID_SLF4J_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_SLF4J_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_SLF4J_FILTER_PREFIX)
public Slf4jLogFilter slf4jLogFilter() {
log.debug("druid slf4j-filter init...");
return new Slf4jLogFilter();
}
@ConditionalOnProperty(prefix = DRUID_LOG4J_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_LOG4J_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_LOG4J_FILTER_PREFIX)
public Log4jFilter log4jFilter() {
log.debug("druid log4j-filter init...");
return new Log4jFilter();
}
@ConditionalOnProperty(prefix = DRUID_LOG4J2_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_LOG4J2_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_LOG4J2_FILTER_PREFIX)
public Log4j2Filter log4j2Filter() {
log.debug("druid log4j2-filter init...");
return new Log4j2Filter();
}
@ConditionalOnProperty(prefix = DRUID_COMMONS_LOG_FILTER_PREFIX, name = "enabled", havingValue = "true")
@Bean
@ConditionalOnProperty(prefix = DRUID_COMMONS_LOG_FILTER_PREFIX, name = "enabled", havingValue = "true")
@ConfigurationProperties(DRUID_COMMONS_LOG_FILTER_PREFIX)
public CommonsLogFilter commonsLogFilter() {
log.debug("druid commons-log-filter init...");
return new CommonsLogFilter();
}
@ConditionalOnMissingBean(DataSource.class)
@Bean(initMethod = "init", destroyMethod = "close")
@ConditionalOnMissingBean(DataSource.class)
public DruidDataSource dataSource() {
log.debug("druid data-source init...");
return new DruidParentDataSource() {};
@@ -2,6 +2,7 @@ package com.github.trang.druid;
import com.github.trang.druid.datasource.init.DruidDataSourceInitializer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -24,7 +25,8 @@ import javax.sql.DataSource;
* @author trang
*/
@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@AutoConfigureAfter(DruidAutoConfiguration.class)
@ConditionalOnBean(DataSource.class)
public class DruidDataSourceInitializerAutoConfiguration {
@@ -19,6 +19,7 @@ import static com.github.trang.druid.properties.DruidServletProperties.DRUID_STA
* @author trang
*/
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = DRUID_STAT_VIEW_SERVLET_PREFIX, name = "enabled", havingValue = "true")
@EnableConfigurationProperties(DruidStatViewServletProperties.class)
public class DruidServletConfiguration {
@@ -30,7 +31,6 @@ public class DruidServletConfiguration {
* 1. 提供监控信息展示的 HTML 页面
* 2. 提供监控信息的 JSON API
*/
@ConditionalOnProperty(prefix = DRUID_STAT_VIEW_SERVLET_PREFIX, name = "enabled", havingValue = "true")
@Bean
public ServletRegistrationBean servletRegistrationBean(DruidStatViewServletProperties properties) {
log.debug("druid stat-view-servlet init...");
@@ -16,6 +16,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import static com.github.trang.druid.properties.DruidStatProperties.DRUID_AOP_STAT_PREFIX;
@@ -32,6 +33,7 @@ public class DruidStatConfiguration {
/**
* 用于采集 Spring 和 JDBC 关联监控的数据
*/
@Configuration
@ConditionalOnClass(Advice.class)
@ConditionalOnProperty(prefix = DRUID_AOP_STAT_PREFIX, name = "enabled", havingValue = "true")
public static class DruidAopStatConfiguration {
@@ -65,12 +67,13 @@ public class DruidStatConfiguration {
/**
* 用于采集 Web 和 JDBC 关联监控的数据
*/
@Configuration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = DRUID_WEB_STAT_PREFIX, name = "enabled", havingValue = "true")
public static class DruidWebStatConfiguration {
private static final Logger log = LoggerFactory.getLogger(DruidStatConfiguration.class);
@ConditionalOnProperty(prefix = DRUID_WEB_STAT_PREFIX, name = "enabled", havingValue = "true")
@Bean
public FilterRegistrationBean filterRegistrationBean(DruidWebStatProperties properties) {
log.debug("druid web-stat-filter init...");
+6
View File
@@ -37,6 +37,7 @@
<module>druid-spring-boot-starter</module>
<module>druid-spring-boot-autoconfigure</module>
<module>druid-spring-boot-samples</module>
<module>druid-spring-boot-actuator</module>
</modules>
<properties>
@@ -75,6 +76,11 @@
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>