diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.java index c343e5a4a7..9dcc5bfb24 100644 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.java +++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.java @@ -16,8 +16,17 @@ package com.alibaba.druid.spring.boot.autoconfigure; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.spring.boot.autoconfigure.metadata.DruidDataSourcePoolMetadata; +import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; +import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration; +import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidSpringAopConfiguration; +import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidStatViewServletConfiguration; +import com.alibaba.druid.spring.boot.autoconfigure.stat.DruidWebStatFilterConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata; import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration; @@ -25,7 +34,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.core.env.Environment; import javax.sql.DataSource; @@ -34,37 +42,23 @@ import javax.sql.DataSource; */ @Configuration @ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class) -@EnableConfigurationProperties({DruidStatProperties.class, DruidDataSourceProperties.class}) -@Import({DruidSpringAopConfiguration.class, DruidStatViewServletConfiguration.class, DruidWebStatFilterConfiguration.class}) +@AutoConfigureBefore(DataSourceAutoConfiguration.class) +@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class}) +@Import({DruidSpringAopConfiguration.class, + DruidStatViewServletConfiguration.class, + DruidWebStatFilterConfiguration.class, + DruidFilterConfiguration.class}) public class DruidDataSourceAutoConfigure { @Bean @ConditionalOnMissingBean - public DataSource dataSource(DruidDataSourceProperties properties, Environment env) { - - //if not found prefix 'spring.datasource.druid' jdbc properties ,'spring.datasource' prefix jdbc properties will be used. - if (properties.getUsername() == null) { - properties.setUsername(env.getProperty("spring.datasource.username")); - } - if (properties.getPassword() == null) { - properties.setPassword(env.getProperty("spring.datasource.password")); - } - if (properties.getUrl() == null) { - properties.setUrl(env.getProperty("spring.datasource.url")); - } - if (properties.getDriverClassName() == null) { - properties.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); - } - - DruidDataSource dataSource = DruidDataSourceBuilder - .create() - .build(properties); - - return dataSource; + public DataSource dataSource() { + return new DruidDataSourceWrapper(); } /** * Register the {@link DataSourcePoolMetadataProvider} instances to support DataSource metrics. + * * @see DataSourcePoolMetadataProvidersConfiguration */ @Bean @@ -73,7 +67,7 @@ public class DruidDataSourceAutoConfigure { @Override public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) { if (dataSource instanceof DruidDataSource) { - return new DruidDataSourcePoolMetadata((DruidDataSource)dataSource); + return new DruidDataSourcePoolMetadata((DruidDataSource) dataSource); } return null; } diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceBuilder.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceBuilder.java index bb38858958..57c66a0068 100644 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceBuilder.java +++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceBuilder.java @@ -19,8 +19,6 @@ package com.alibaba.druid.spring.boot.autoconfigure; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.core.env.Environment; -import java.sql.SQLException; - /** * @author lihengming [89921218@qq.com] */ @@ -30,116 +28,29 @@ public class DruidDataSourceBuilder { return new DruidDataSourceBuilder(); } - @Deprecated + /** + * For build multiple DruidDataSource, detail see document. + */ public DruidDataSource build() { - return new DruidDataSource(); + return new DruidDataSourceWrapper(); } /** - * Use Spring Environment by specify configuration properties prefix to build DruidDataSource. + * For issue #1796, use Spring Environment by specify configuration properties prefix to build DruidDataSource. *

- * Environment support .properties, .yml ,command etc. - *

- * Properties key style support max-active,MAX_ACTIVE,maxActive etc. - *

- * Please read the spring documents for details. - * @param env Spring Environment - * @param prefix Spring Boot configuration properties prefix - * @return DruidDataSource + * 这是为了兼容 Spring Boot 1.X 中 .properties 内配置属性不能按照配置声明顺序进行绑定,进而导致配置出错(issue #1796 )而提供的方法。 + * 如果你不存在上述问题或者使用 .yml 进行配置则不必使用该方法,使用上面的{@link DruidDataSourceBuilder#build}即可,Spring Boot 2.0 修复了该问题,该方法届时也会停用。 */ public DruidDataSource build(Environment env, String prefix) { - DruidDataSourceProperties properties = new DruidDataSourceProperties(); - properties.setUrl(env.getProperty(prefix + "url")); - properties.setUsername(env.getProperty(prefix + "username")); - properties.setPassword(env.getProperty(prefix + "password")); - properties.setDriverClassName(env.getProperty(prefix + "driver-class-name")); - properties.setInitialSize(env.getProperty(prefix + "initial-size", Integer.class)); - properties.setMaxActive(env.getProperty(prefix + "max-active", Integer.class)); - properties.setMinIdle(env.getProperty(prefix + "min-idle", Integer.class)); - properties.setMaxWait(env.getProperty(prefix + "max-wait", Long.class)); - properties.setPoolPreparedStatements(env.getProperty(prefix + "pool-prepared-statements", Boolean.class)); - properties.setMaxOpenPreparedStatements(env.getProperty(prefix + "max-open-prepared-statements", Integer.class)); - properties.setMaxPoolPreparedStatementPerConnectionSize( - env.getProperty(prefix + "max-pool-prepared-statement-per-connection-size", Integer.class)); - properties.setValidationQuery(env.getProperty(prefix + "validation-query")); - properties.setValidationQueryTimeout(env.getProperty(prefix + "validation-query-timeout", Integer.class)); - properties.setTestOnBorrow(env.getProperty(prefix + "test-on-borrow", Boolean.class)); - properties.setTestOnReturn(env.getProperty(prefix + "test-on-return", Boolean.class)); - properties.setTestWhileIdle(env.getProperty(prefix + "test-while-idle", Boolean.class)); - properties.setTimeBetweenEvictionRunsMillis(env.getProperty(prefix + "time-between-eviction-runs-millis", Long.class)); - properties.setMinEvictableIdleTimeMillis(env.getProperty(prefix + "min-evictable-idle-time-millis", Long.class)); - properties.setMaxEvictableIdleTimeMillis(env.getProperty(prefix + "max-evictable-idle-time-millis", Long.class)); - properties.setFilters(env.getProperty(prefix + "filters")); - return build(properties); - } - - - DruidDataSource build(DruidDataSourceProperties properties) { - DruidDataSource dataSource = new DruidDataSource(); - if (properties.getUrl() != null) { - dataSource.setUrl(properties.getUrl()); - } - if (properties.getUsername() != null) { - dataSource.setUsername(properties.getUsername()); - } - if (properties.getPassword() != null) { - dataSource.setPassword(properties.getPassword()); - } - if (properties.getDriverClassName() != null) { - dataSource.setDriverClassName(properties.getDriverClassName()); - } - if (properties.getInitialSize() != null) { - dataSource.setInitialSize(properties.getInitialSize()); - } - if (properties.getMaxActive() != null) { - dataSource.setMaxActive(properties.getMaxActive()); - } - if (properties.getMinIdle() != null) { - dataSource.setMinIdle(properties.getMinIdle()); - } - if (properties.getMaxWait() != null) { - dataSource.setMaxWait(properties.getMaxWait()); - } - if (properties.getPoolPreparedStatements() != null) { - dataSource.setPoolPreparedStatements(properties.getPoolPreparedStatements()); - } - if (properties.getMaxOpenPreparedStatements() != null) { - dataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements()); - } - if (properties.getMaxPoolPreparedStatementPerConnectionSize() != null) { - dataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize()); - } - if (properties.getValidationQuery() != null) { - dataSource.setValidationQuery(properties.getValidationQuery()); - } - if (properties.getValidationQueryTimeout() != null) { - dataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout()); - } - if (properties.getTestWhileIdle() != null) { - dataSource.setTestWhileIdle(properties.getTestWhileIdle()); - } - if (properties.getTestOnBorrow() != null) { - dataSource.setTestOnBorrow(properties.getTestOnBorrow()); - } - if (properties.getTestOnReturn() != null) { - dataSource.setTestOnReturn(properties.getTestOnReturn()); - } - if (properties.getTimeBetweenEvictionRunsMillis() != null) { - dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis()); - } - if (properties.getMinEvictableIdleTimeMillis() != null) { - dataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis()); - } - if (properties.getMaxEvictableIdleTimeMillis() != null) { - dataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis()); - } - try { - if (properties.getFilters() != null) { - dataSource.setFilters(properties.getFilters()); - } - } catch (SQLException e) { - e.printStackTrace(); - } - return dataSource; + DruidDataSource druidDataSource = new DruidDataSourceWrapper(); + druidDataSource.setMinEvictableIdleTimeMillis( + env.getProperty(prefix + "min-evictable-idle-time-millis", + Long.class, + DruidDataSource.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS)); + druidDataSource.setMaxEvictableIdleTimeMillis( + env.getProperty(prefix + "max-evictable-idle-time-millis", + Long.class, + DruidDataSource.DEFAULT_MAX_EVICTABLE_IDLE_TIME_MILLIS)); + return druidDataSource; } } diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourcePoolMetadata.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourcePoolMetadata.java deleted file mode 100644 index f890f1e468..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourcePoolMetadata.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.alibaba.druid.spring.boot.autoconfigure; - -import com.alibaba.druid.pool.DruidDataSource; -import org.springframework.boot.autoconfigure.jdbc.metadata.AbstractDataSourcePoolMetadata; -import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata; - -/** - * {@link DataSourcePoolMetadata} for a Druid DataSource. - * - * @author lihengming [89921218@qq.com] - */ -public class DruidDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata { - /** - * Create an instance with the data source to use. - * - * @param dataSource the data source - */ - public DruidDataSourcePoolMetadata(DruidDataSource dataSource) { - super(dataSource); - } - - @Override - public Integer getActive() { - return getDataSource().getActiveCount(); - } - - @Override - public Integer getMax() { - return getDataSource().getMaxActive(); - } - - @Override - public Integer getMin() { - return getDataSource().getMinIdle(); - } - - @Override - public String getValidationQuery() { - return getDataSource().getValidationQuery(); - } -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceProperties.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceProperties.java deleted file mode 100644 index ba7d898d22..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceProperties.java +++ /dev/null @@ -1,210 +0,0 @@ -package com.alibaba.druid.spring.boot.autoconfigure; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * @author lihengming [89921218@qq.com] - */ -@ConfigurationProperties("spring.datasource.druid") -public class DruidDataSourceProperties { - private String url; - - private String username; - - private String password; - - private String driverClassName; - - private Integer initialSize; - - private Integer maxActive; - - private Integer minIdle; - - private Long maxWait; - - private Boolean poolPreparedStatements; - - private Integer maxOpenPreparedStatements; - - private Integer maxPoolPreparedStatementPerConnectionSize; - - private String validationQuery; - - private Integer validationQueryTimeout; - - private Boolean testOnBorrow; - - private Boolean testOnReturn; - - private Boolean testWhileIdle; - - private Long timeBetweenEvictionRunsMillis; - - private Long minEvictableIdleTimeMillis; - - private Long maxEvictableIdleTimeMillis; - - private String filters = "stat"; - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getDriverClassName() { - return driverClassName; - } - - public void setDriverClassName(String driverClassName) { - this.driverClassName = driverClassName; - } - - public Integer getInitialSize() { - return initialSize; - } - - public void setInitialSize(Integer initialSize) { - this.initialSize = initialSize; - } - - public Integer getMaxActive() { - return maxActive; - } - - public void setMaxActive(Integer maxActive) { - this.maxActive = maxActive; - } - - public Integer getMinIdle() { - return minIdle; - } - - public void setMinIdle(Integer minIdle) { - this.minIdle = minIdle; - } - - public Long getMaxWait() { - return maxWait; - } - - public void setMaxWait(Long maxWait) { - this.maxWait = maxWait; - } - - public Boolean getPoolPreparedStatements() { - return poolPreparedStatements; - } - - public void setPoolPreparedStatements(Boolean poolPreparedStatements) { - this.poolPreparedStatements = poolPreparedStatements; - } - - public Integer getMaxOpenPreparedStatements() { - return maxOpenPreparedStatements; - } - - public void setMaxOpenPreparedStatements(Integer maxOpenPreparedStatements) { - this.maxOpenPreparedStatements = maxOpenPreparedStatements; - } - - public Integer getMaxPoolPreparedStatementPerConnectionSize() { - return maxPoolPreparedStatementPerConnectionSize; - } - - public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) { - this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize; - } - - public String getValidationQuery() { - return validationQuery; - } - - public void setValidationQuery(String validationQuery) { - this.validationQuery = validationQuery; - } - - public Integer getValidationQueryTimeout() { - return validationQueryTimeout; - } - - public void setValidationQueryTimeout(Integer validationQueryTimeout) { - this.validationQueryTimeout = validationQueryTimeout; - } - - public Boolean getTestOnBorrow() { - return testOnBorrow; - } - - public void setTestOnBorrow(Boolean testOnBorrow) { - this.testOnBorrow = testOnBorrow; - } - - public Boolean getTestOnReturn() { - return testOnReturn; - } - - public void setTestOnReturn(Boolean testOnReturn) { - this.testOnReturn = testOnReturn; - } - - public Boolean getTestWhileIdle() { - return testWhileIdle; - } - - public void setTestWhileIdle(Boolean testWhileIdle) { - this.testWhileIdle = testWhileIdle; - } - - public Long getTimeBetweenEvictionRunsMillis() { - return timeBetweenEvictionRunsMillis; - } - - public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) { - this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; - } - - public Long getMinEvictableIdleTimeMillis() { - return minEvictableIdleTimeMillis; - } - - public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) { - this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; - } - - public Long getMaxEvictableIdleTimeMillis() { - return maxEvictableIdleTimeMillis; - } - - public void setMaxEvictableIdleTimeMillis(Long maxEvictableIdleTimeMillis) { - this.maxEvictableIdleTimeMillis = maxEvictableIdleTimeMillis; - } - - public String getFilters() { - return filters; - } - - public void setFilters(String filters) { - this.filters = filters; - } - -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidSpringAopConfiguration.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidSpringAopConfiguration.java deleted file mode 100644 index fb1fc7140a..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidSpringAopConfiguration.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2017 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.spring.boot.autoconfigure; - -import com.alibaba.druid.support.spring.stat.DruidStatInterceptor; -import org.aopalliance.aop.Advice; -import org.springframework.aop.Advisor; -import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; -import org.springframework.aop.support.RegexpMethodPointcutAdvisor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; - -/** - * @author lihengming [89921218@qq.com] - */ -@ConditionalOnProperty("spring.datasource.druid.aop-patterns") -public class DruidSpringAopConfiguration { - @Value("${spring.aop.proxy-target-class:false}") - private boolean proxyTargetClass; - - @Bean - public Advice advice() { - return new DruidStatInterceptor(); - } - - @Bean - public Advisor advisor(DruidStatProperties properties) { - return new RegexpMethodPointcutAdvisor(properties.getAopPatterns(), advice()); - } - - @Bean - public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { - DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); - advisorAutoProxyCreator.setProxyTargetClass(proxyTargetClass); - return advisorAutoProxyCreator; - } -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatProperties.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatProperties.java deleted file mode 100644 index aa3b31ecb0..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatProperties.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright 1999-2017 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.spring.boot.autoconfigure; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * @author lihengming [89921218@qq.com] - */ -@ConfigurationProperties("spring.datasource.druid") -public class DruidStatProperties { - private String[] aopPatterns; - private StatViewServlet statViewServlet = new StatViewServlet(); - private WebStatFilter webStatFilter = new WebStatFilter(); - - - public String[] getAopPatterns() { - return aopPatterns; - } - - public void setAopPatterns(String[] aopPatterns) { - this.aopPatterns = aopPatterns; - } - - public StatViewServlet getStatViewServlet() { - return statViewServlet; - } - - public void setStatViewServlet(StatViewServlet statViewServlet) { - this.statViewServlet = statViewServlet; - } - - public WebStatFilter getWebStatFilter() { - return webStatFilter; - } - - public void setWebStatFilter(WebStatFilter webStatFilter) { - this.webStatFilter = webStatFilter; - } - - public static class StatViewServlet { - /** - * Enable StatViewServlet. - */ - private boolean enabled = true; - private String urlPattern; - private String allow; - private String deny; - private String loginUsername; - private String loginPassword; - private String resetEnable; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public String getUrlPattern() { - return urlPattern; - } - - public void setUrlPattern(String urlPattern) { - this.urlPattern = urlPattern; - } - - public String getAllow() { - return allow; - } - - public void setAllow(String allow) { - this.allow = allow; - } - - public String getDeny() { - return deny; - } - - public void setDeny(String deny) { - this.deny = deny; - } - - public String getLoginUsername() { - return loginUsername; - } - - public void setLoginUsername(String loginUsername) { - this.loginUsername = loginUsername; - } - - public String getLoginPassword() { - return loginPassword; - } - - public void setLoginPassword(String loginPassword) { - this.loginPassword = loginPassword; - } - - public String getResetEnable() { - return resetEnable; - } - - public void setResetEnable(String resetEnable) { - this.resetEnable = resetEnable; - } - } - - public static class WebStatFilter { - /** - * Enable WebStatFilter. - */ - private boolean enabled = true; - private String urlPattern; - private String exclusions; - private String sessionStatMaxCount; - private String sessionStatEnable; - private String principalSessionName; - private String principalCookieName; - private String profileEnable; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public String getUrlPattern() { - return urlPattern; - } - - public void setUrlPattern(String urlPattern) { - this.urlPattern = urlPattern; - } - - public String getExclusions() { - return exclusions; - } - - public void setExclusions(String exclusions) { - this.exclusions = exclusions; - } - - public String getSessionStatMaxCount() { - return sessionStatMaxCount; - } - - public void setSessionStatMaxCount(String sessionStatMaxCount) { - this.sessionStatMaxCount = sessionStatMaxCount; - } - - public String getSessionStatEnable() { - return sessionStatEnable; - } - - public void setSessionStatEnable(String sessionStatEnable) { - this.sessionStatEnable = sessionStatEnable; - } - - public String getPrincipalSessionName() { - return principalSessionName; - } - - public void setPrincipalSessionName(String principalSessionName) { - this.principalSessionName = principalSessionName; - } - - public String getPrincipalCookieName() { - return principalCookieName; - } - - public void setPrincipalCookieName(String principalCookieName) { - this.principalCookieName = principalCookieName; - } - - public String getProfileEnable() { - return profileEnable; - } - - public void setProfileEnable(String profileEnable) { - this.profileEnable = profileEnable; - } - } -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatViewServletConfiguration.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatViewServletConfiguration.java deleted file mode 100644 index d2d9c6178a..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidStatViewServletConfiguration.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2017 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.spring.boot.autoconfigure; - -import com.alibaba.druid.support.http.StatViewServlet; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.web.servlet.ServletRegistrationBean; -import org.springframework.context.annotation.Bean; - -/** - * @author lihengming [89921218@qq.com] - */ -@ConditionalOnWebApplication -@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true) -public class DruidStatViewServletConfiguration { - @Bean - public ServletRegistrationBean servletRegistrationBean(DruidStatProperties properties) { - DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); - ServletRegistrationBean registrationBean = new ServletRegistrationBean(); - registrationBean.setServlet(new StatViewServlet()); - registrationBean.addUrlMappings(config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"); - if (config.getAllow() != null) { - registrationBean.addInitParameter("allow", config.getAllow()); - } - if (config.getDeny() != null) { - registrationBean.addInitParameter("deny", config.getDeny()); - } - if (config.getLoginUsername() != null) { - registrationBean.addInitParameter("loginUsername", config.getLoginUsername()); - } - if (config.getLoginPassword() != null) { - registrationBean.addInitParameter("loginPassword", config.getLoginPassword()); - } - if (config.getResetEnable() != null) { - registrationBean.addInitParameter("resetEnable", config.getResetEnable()); - } - return registrationBean; - } -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidWebStatFilterConfiguration.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidWebStatFilterConfiguration.java deleted file mode 100644 index 85be259ef6..0000000000 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidWebStatFilterConfiguration.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1999-2017 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.druid.spring.boot.autoconfigure; - -import com.alibaba.druid.support.http.WebStatFilter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; - -/** - * @author lihengming [89921218@qq.com] - */ -@ConditionalOnWebApplication -@ConditionalOnProperty(name = "spring.datasource.druid.web-stat-filter.enabled", havingValue = "true", matchIfMissing = true) -public class DruidWebStatFilterConfiguration { - @Bean - public FilterRegistrationBean filterRegistrationBean(DruidStatProperties properties) { - DruidStatProperties.WebStatFilter config = properties.getWebStatFilter(); - FilterRegistrationBean registrationBean = new FilterRegistrationBean(); - WebStatFilter filter = new WebStatFilter(); - registrationBean.setFilter(filter); - registrationBean.addUrlPatterns(config.getUrlPattern() != null ? config.getUrlPattern() : "/*"); - registrationBean.addInitParameter("exclusions", config.getExclusions() != null ? config.getExclusions() : "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); - if (config.getSessionStatEnable() != null) { - registrationBean.addInitParameter("sessionStatEnable", config.getSessionStatEnable()); - } - if (config.getSessionStatMaxCount() != null) { - registrationBean.addInitParameter("sessionStatMaxCount", config.getSessionStatMaxCount()); - } - if (config.getPrincipalSessionName() != null) { - registrationBean.addInitParameter("principalSessionName", config.getPrincipalSessionName()); - } - if (config.getPrincipalCookieName() != null) { - registrationBean.addInitParameter("principalCookieName", config.getPrincipalCookieName()); - } - if (config.getProfileEnable() != null) { - registrationBean.addInitParameter("profileEnable", config.getProfileEnable()); - } - return registrationBean; - } -} diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.java new file mode 100644 index 0000000000..94a2f4d37c --- /dev/null +++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/stat/DruidFilterConfiguration.java @@ -0,0 +1,119 @@ +/* + * Copyright 1999-2017 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.spring.boot.autoconfigure.stat; + +import com.alibaba.druid.filter.config.ConfigFilter; +import com.alibaba.druid.filter.encoding.EncodingConvertFilter; +import com.alibaba.druid.filter.logging.*; +import com.alibaba.druid.filter.stat.StatFilter; +import com.alibaba.druid.wall.WallConfig; +import com.alibaba.druid.wall.WallFilter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * @author lihengming [89921218@qq.com] + */ +public class DruidFilterConfiguration { + + @Bean + @ConfigurationProperties(FILTER_STAT_PREFIX) + @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = FILTER_CONFIG_PREFIX, name = "enabled", matchIfMissing = true) + public StatFilter statFilter() { + return new StatFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_CONFIG_PREFIX) + @ConditionalOnProperty(prefix = FILTER_CONFIG_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public ConfigFilter configFilter() { + return new ConfigFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_ENCODING_PREFIX) + @ConditionalOnProperty(prefix = FILTER_ENCODING_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public EncodingConvertFilter encodingConvertFilter() { + return new EncodingConvertFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_SLF4J_PREFIX) + @ConditionalOnProperty(prefix = FILTER_SLF4J_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public Slf4jLogFilter slf4jLogFilter() { + return new Slf4jLogFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_LOG4J_PREFIX) + @ConditionalOnProperty(prefix = FILTER_LOG4J_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public Log4jFilter log4jFilter() { + return new Log4jFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_LOG4J2_PREFIX) + @ConditionalOnProperty(prefix = FILTER_LOG4J2_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public Log4j2Filter log4j2Filter() { + return new Log4j2Filter(); + } + + @Bean + @ConfigurationProperties(FILTER_COMMONS_LOG_PREFIX) + @ConditionalOnProperty(prefix = FILTER_COMMONS_LOG_PREFIX, name = "enabled") + public CommonsLogFilter commonsLogFilter() { + return new CommonsLogFilter(); + } + + @Bean + @ConfigurationProperties(FILTER_WALL_CONFIG_PREFIX) + @ConditionalOnProperty(prefix = FILTER_WALL_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public WallConfig wallConfig() { + return new WallConfig(); + } + + @Bean + @ConfigurationProperties(FILTER_WALL_PREFIX) + @ConditionalOnProperty(prefix = FILTER_WALL_PREFIX, name = "enabled") + @ConditionalOnMissingBean + public WallFilter wallFilter(WallConfig wallConfig) { + WallFilter filter = new WallFilter(); + filter.setConfig(wallConfig); + return filter; + } + + + private static final String FILTER_STAT_PREFIX = "spring.datasource.druid.filter.stat"; + private static final String FILTER_CONFIG_PREFIX = "spring.datasource.druid.filter.config"; + private static final String FILTER_ENCODING_PREFIX = "spring.datasource.druid.filter.encoding"; + private static final String FILTER_SLF4J_PREFIX = "spring.datasource.druid.filter.slf4j"; + private static final String FILTER_LOG4J_PREFIX = "spring.datasource.druid.filter.log4j"; + private static final String FILTER_LOG4J2_PREFIX = "spring.datasource.druid.filter.log4j2"; + private static final String FILTER_COMMONS_LOG_PREFIX = "spring.datasource.druid.filter.commons-log"; + private static final String FILTER_WALL_PREFIX = "spring.datasource.druid.filter.wall"; + private static final String FILTER_WALL_CONFIG_PREFIX = FILTER_WALL_PREFIX + ".config"; + + +}