gpt4 book ai didi

java - Spring中未加载Bean方法 'dataSource'

转载 作者:行者123 更新时间:2023-12-01 06:22:09 26 4
gpt4 key购买 nike

我尝试在 Spring 框架中访问 MySQL 数据库,但收到以下错误。

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

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' 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.

这是我的代码:

pom.xml 依赖项

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

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

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

应用程序属性

# ===============================
# = DATA SOURCE
# ===============================

# Set here configurations for the database connection
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc.mysql://localhost:3306/geektextdatabase

# Username and password
spring.datasource.username=root
spring.datasource.password=littlegraycells


# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.tomcat.test-while-idle= true
spring.datasource.tomcat.validation-query= SELECT 1


# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.database=MYSQL
spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

adminportalapplication.java

package com.adminportal;

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 AdminportalApplication {

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

有人可以帮助我,告诉我我错过了什么吗?我缺少任何依赖项吗?我使用的是 Java 9,我不知道这是否会造成问题。

最佳答案

查看错误,有一个 spring 配置类 HibernateJpaAutoConfiguration (它位于依赖项的类路径中),它依赖于未找到的数据源 bean

@Autowired
private DataSource dataSource;

因此,简而言之,您的 DataSource bean 定义了吗?如果没有在配置文件中定义(或在 AdminportalApplication.java 中)

@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

阅读more here how to configure a datasource

关于java - Spring中未加载Bean方法 'dataSource',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48756576/

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