gpt4 book ai didi

mysql - 由于找不到 'javax.sql.DataSource',无法启动我的 Spring Boot 应用程序

转载 作者:行者123 更新时间:2023-11-29 18:31:12 24 4
gpt4 key购买 nike

我是 Spring Boot 的初学者,正在尝试编写一个简单的 Spring Boot 应用程序。我的文件夹结构如下:

  -> Project

-> build.gradle

-> settings.gradle

-> src/main/java

-> package

-> Main.java

-> UserController.java

-> UserRespository.java

-> dto

-> User.java

->src/main/resouces

-> application.properties

我的build.gradle如下:

    buildscript {
repositories {
jcenter()
}
dependencies {
classpath(
'org.springframework.boot:spring-boot-gradle- plugin:1.5.6.RELEASE'

)
classpath('mysql:mysql-connector-java:5.1.34')
}
}

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile(
'org.springframework.boot:spring-boot-starter-actuator',
'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-data-jpa'
)
compile('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

application.properties如下:

   spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/user
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database=MYSQL
spring.jpa.show-sql = true

我的Main.java如下:-

  import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
};

}

我能够成功构建该应用程序。如果我运行该应用程序,我将无法使用以下堆栈跟踪启动应用程序:

     2017-08-14 20:43:02.976  WARN 27205 --- [           main]  ationConfigEmbeddedWebApplicationContext : Exception encountered during context  initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2017-08-14 20:43:02.978 INFO 27205 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-08-14 20:43:03.007 INFO 27205 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-14 20:43:03.198 ERROR 27205 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

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

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.Data Source' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

我已经检查了依赖关系树,可以在其中找到 hibernate 和 mysql 连接器。尝试删除 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) ,在这种情况下我得到 Cannot load driver class: com.mysql.jdbc.Driver

最佳答案

您应该将 dto 包移至您的 Main.java 类包中,在您的情况下,它应该类似于 src/main/java/package/dto

因此,当 spring-boot 扫描时,您的实体将对扫描仪可见。

确保您已添加 MySQL 驱动程序依赖项

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

关于mysql - 由于找不到 'javax.sql.DataSource',无法启动我的 Spring Boot 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com