Affects: \
Comment From: 780886
No data source error `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 (no profiles are currently active).`
Handwritten starter tool class to reproduce problems
`
</dependency>`
question: According to the semantic understanding of @ ConditionalOnClass (DruidDataSource. class), it is impossible to report an error. If it does not exist, the object will not be initialized. Why would an error be reported?
conclusion: As long as a bean such as @ ConditionalOnClass (DruidDataSource. class) exists in the @ Configuration class, if the class does not exist at runtime, it will cause other beans in the entire class to fail to initialize.It is recommended not to obtain beans through JDK reflection, but rather through bytecode format!
Comment From: snicoll
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: 780886
I think this is more like a bug, which will cause Spring to be unable to initialize beans. Looking at the following code example, as long as a bean like @ ConditionalOnClass (DruidDataSource. class) exists in the @ Configuration class, at runtime, if that class does not exist, it will cause other beans in the entire class to fail to initialize
`package com.lesson.commons.config.wrong;
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ClassNotFoundConfig { @Bean public ExistingClass existingClass() { return new ExistingClass(); } @Bean(name = "sparrow_default") @ConditionalOnClass(DruidDataSource.class) public DruidDataSource druidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); return druidDataSource; } }`
Comment From: snicoll
Please don't duplicate the issue, it is a waste of time for everybody involved. The javadoc of @ConditionalOnClass
explains you can't put @ConditionalOnClass
on bean methods. Here's another duplicate with an example: https://github.com/spring-projects/spring-boot/issues/35700
Comment From: 780886
Okay, thank you very much