java - Spring 文档中最简单的示例不起作用。“考虑在你的配置中定义一个 'xxxRepository' 类型的 bean。”
问题描述
我真的越来越累了。一整天我都在尝试使用 DB 对应用程序进行最简单的配置,但没有任何效果,即使我 1:1 复制教程也是如此。
https://spring.io/guides/gs/accessing-data-mysql/
从提供的包中复制所有 3 个类(MainController
, UserRepository
, User
+ pom.xml
)后,更改数据库配置application.properties
,我收到以下错误:
Description:
Field userRepository in com.example.testfield.MainController required a bean of type 'com.example.testfield.UserRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.testfield.UserRepository' in your configuration.
当我添加@EnableJpaRepositories
并强制扫描时TestFieldApplication
,我得到:@Repository
UserRepository
Field userRepository in com.example.testfield.MainController required a bean named 'entityManagerFactory' that could not be found.
当我尝试以entityManager
这种方式手动添加时:
@Configuration
public class ApplicationConfiguration {
@Bean
public DataSource getDataSource() {
return DataSourceBuilder.create()
.driverClassName("com.mysql.jdbc.Driver")
.url("jdbc:mysql://localhost:3306/xxx")
.username("xxx")
.password("xxx")
.build();
}
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
return vendorAdapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(createJpaVendorAdapter());
factory.setPackagesToScan("com.example.TestField.entity");
factory.setDataSource(getDataSource());
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory){
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
然后我有一个问题说“找不到支持的数据源类型”。
我尝试了一切 - 用新用户创建一个新数据库,更改每个依赖项的版本,将属性放在application.properties
......没有任何效果。我很沮丧,因为它是如此基本的东西,我尝试了一些教程,复制它们,但没有任何效果,难以置信!
附言。从 Intellij 插件连接时,我使用了相同的 url/user/password 来测试连接,它工作正常。
编辑:添加其他类
package com.example.testfield;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestField {
public static void main(String[] args) {
SpringApplication.run(TestField.class, args);
}
}
package com.example.testfield;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>accessing-data-mysql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TestField</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方案
好的,最后我能够解决问题。解决方案是将 getDataSource 方法的返回类型从:
@Bean
public DataSource getDataSource() {
return DataSourceBuilder.create()
.url("xxx")
(...)
.build();
}
至:
@Bean
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUrl("xxx")
(...)
return ds;
}
有点混乱,互联网上的大多数教程都显示第一种方式。感谢所有试图提供帮助的人!:)
推荐阅读
- c# - 在 EF Core 中分解结构?
- java - Java 未经检查强制转换为类型参数警告,尽管它是
- here-api - 平台数据扩展:如何解析 ROAD_NAME_FCN
- python - 在 GPU 上使用 tensorflow 训练模型,使用 Adadelta 优化器不起作用。但是当我用 Adam 替换 Adadelta 时,它似乎没有问题。
- sql - SQL 显示 2 个值不同并匹配第一个值的顺序
- javafx - 使用 fxml 在 javaFx 中的 TableView 中启用复选框
- java - 如何通知一个线程,另一个线程已完成执行?
- excel - Word VBA:更新 OLE 源链接
- c# - 来自 DataContext 的 Hangfire 身份验证
- javascript - 从 EventEmitter 创建流