DruidDataSourceWrapper必须要获取到环境变量否则抛异常

afdcj2ne  于 3个月前  发布在  Druid
关注(0)|答案(4)|浏览(50)

已经在 applicationContext.xml 中配置了datasource,没有在 application.yml 中配置 spring.datasource 属性

<beans>
        <bean class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" id="gaussDataSource"
              init-method="init">
            <property name="driverClassName" value="${datasource.jdbcDriverClass.6}"/>
            <property name="url" value="${datasource.url.6}"/>
            <property name="username" value="${datasource.user.6}"/>
            <property name="password" value="${datasource.password.6}"/>

但是 DruidDataSourceAutoConfigure 构造 DruidDataSourceWrapper ,其中必须要获取环境变量

@Override
    public void afterPropertiesSet() throws Exception {
        //if not found prefix 'spring.datasource.druid' jdbc properties ,'spring.datasource' prefix jdbc properties will be used.
        if (super.getUsername() == null) {
            super.setUsername(basicProperties.determineUsername());
        }

没有就会抛异常 Method threw 'org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException' exception.
最终导致

nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (the profiles uat are currently active).

现在必须要加注解 @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class) 才能正常启动使用,但是监控Servlet就不会自动装配了

ar5n3qh5

ar5n3qh51#

我一开始选用的 dynamic-datasource 发现了这个问题,我现在本来打算试试单库能不能用,发现单库不会报错,但是监控也不会自动装配,我也没有去排除 com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure 现在我得考虑换别的方法了

syqv5f0l

syqv5f0l2#

我想到了一个比较拙劣的解决方案,那就是重写 DruidDataSourceAutoConfigure 配置类:

package org.hub.system.config.druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot3.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.spring.boot3.autoconfigure.stat.DruidFilterConfiguration;
import com.alibaba.druid.spring.boot3.autoconfigure.stat.DruidSpringAopConfiguration;
import com.alibaba.druid.spring.boot3.autoconfigure.stat.DruidStatViewServletConfiguration;
import com.alibaba.druid.spring.boot3.autoconfigure.stat.DruidWebStatFilterConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
@Primary
@Configuration
@ConditionalOnClass({DruidDataSource.class})
@AutoConfigureBefore({DataSourceAutoConfiguration.class})
@EnableConfigurationProperties({DruidStatProperties.class, DataSourceProperties.class})
@Import({DruidSpringAopConfiguration.class,
        DruidStatViewServletConfiguration.class,
        DruidWebStatFilterConfiguration.class,
        DruidFilterConfiguration.class})
public class DruidDataSourceAutoConfigure {
    public DruidDataSourceAutoConfigure() {
    }
}

配置文件剔除默认的,不使用自带的配置类

spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

下面必须,否则不会装配 Servlet

spring.datasource.druid. stat-view-servlet. enabled =true

⚠️ 注:源码中对 DruidStatViewServletConfiguration 的自动装配有条件。

然后,我也成功访问到了监控页面~

k10s72fa

k10s72fa3#

我也遇到了这个问题

mnemlml8

mnemlml84#

***@***.***十分感谢您的来信!

相关问题