已经在 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就不会自动装配了
4条答案
按热度按时间ar5n3qh51#
我一开始选用的
dynamic-datasource
发现了这个问题,我现在本来打算试试单库能不能用,发现单库不会报错,但是监控也不会自动装配,我也没有去排除com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure
现在我得考虑换别的方法了syqv5f0l2#
我想到了一个比较拙劣的解决方案,那就是重写
DruidDataSourceAutoConfigure
配置类:配置文件剔除默认的,不使用自带的配置类
下面必须,否则不会装配
Servlet
⚠️ 注:源码中对
DruidStatViewServletConfiguration
的自动装配有条件。然后,我也成功访问到了监控页面~
k10s72fa3#
我也遇到了这个问题
mnemlml84#
***@***.***十分感谢您的来信!