SpringBoot+JPA 项目启动失败,无法扫描到Repository

我的项目启动时报错:

Description:

Field bannerRepository in com.zhou.wade.service.impl.BannerServiceImpl required a bean of type 'com.zhou.wade.repository.BannerRepository' 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.zhou.wade.repository.BannerRepository' in your configuration.

网上大多数说是扫描不到包,需要手动添加扫描

(1) @ComponentScan(basePackages = "xxx.xxx.xxx"):扫描 @Controller、@Service 注解;
(2) @EnableJpaRepositories(basePackages = "xxx.xxx.xxx"):扫描 @Repository 注解;
(3) @EntityScan(basePackages = "xxx.xxx.xxx"):扫描 @Entity 注解;

尝试了一下,发现都不行。

  1. 最后将exclude = {DataSourceAutoConfiguration.class}删除
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class WadeApplication {
    public static void main(String[] args) {
        SpringApplication.run(WadeApplication.class, args);
    }
}

但是此时又报另一个错,原来自己犯了低级错误。。。

2020-03-19 11:53:54.286  INFO 7852 --- [  restartedMain] com.zhou.wade.WadeApplication            : Starting WadeApplication on USER-20170308ID with PID 7852 (E:\GZ\a\wade\target\classes started by Administrator in E:\GZ\a\wade)
2020-03-19 11:53:54.290  INFO 7852 --- [  restartedMain] com.zhou.wade.WadeApplication            : The following profiles are active: dev
2020-03-19 11:53:54.347  INFO 7852 --- [  restartedMain] o.s.b.devtools.restart.ChangeableUrls    : The Class-Path manifest attribute in E:\IDEAPROJECT\apache-maven-3.6.0\rep\org\glassfish\jaxb\jaxb-runtime\2.3.2\jaxb-runtime-2.3.2.jar referenced one or more files that do not exist: file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.xml.bind-api-2.3.2.jar,file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/txw2-2.3.2.jar,file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/istack-commons-runtime-3.0.8.jar,file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/stax-ex-1.8.1.jar,file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/FastInfoset-1.2.16.jar,file:/E:/IDEAPROJECT/apache-maven-3.6.0/rep/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.activation-api-1.2.1.jar
2020-03-19 11:53:54.347  INFO 7852 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-03-19 11:53:54.347  INFO 7852 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-03-19 11:53:55.147  INFO 7852 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-03-19 11:53:55.238  INFO 7852 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 79ms. Found 2 JPA repository interfaces.
2020-03-19 11:53:55.601  INFO 7852 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-19 11:53:55.929  INFO 7852 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8001 (http)
2020-03-19 11:53:55.940  INFO 7852 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-19 11:53:55.940  INFO 7852 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-03-19 11:53:56.062  INFO 7852 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-19 11:53:56.062  INFO 7852 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1715 ms
2020-03-19 11:53:56.127  WARN 7852 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource [org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]: Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-03-19 11:53:56.130  INFO 7852 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-03-19 11:53:56.139  WARN 7852 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2020-03-19 11:53:56.151  INFO 7852 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-19 11:53:56.161 ERROR 7852 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Failed to configure a DataSource: 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).

url填错了。。。
少了jdbc:mysql指定

spring:
  datasource:
    url: jdbc://localhost:3306/b_wade?characterEncoding=utf-8&serverTimezone=GMT%2B8
    username: root
    password: root
  1. 正确:
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/b_wade?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
  1. 最后还需要指定驱动名
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/b_wade?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
Spring Boot 和 JPA(Java Persistence API)结合通常用于简化企业级应用的开发,它们提供了强大的数据库操作能力和自动配置的功能。以下是创建一个基本的 Spring Boot + JPA 项目的步骤: 1. **设置环境**: - 安装 Java SDK 和 Maven 或 Gradle 构建工具。 - 创建一个新的 Spring Boot 项目,可以通过 IntelliJ IDEA、Eclipse 或命令行工具如 `spring Initializr` 来创建。 2. **添加依赖**: 在你的 `pom.xml` (Maven) 或 `build.gradle` (Gradle) 文件中,添加以下 JPA 和数据源相关的依赖: ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Gradle --> implementation 'org.springframework.boot:spring-boot-starter-data-jpa' ``` 3. **配置数据库**: - 在 `application.properties` 或 `application.yml` 中提供数据库连接信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.jpa.hibernate.ddl-auto=update ``` 4. **实体类定义**: 创建一个或多个代表数据库表的实体类(`MyEntity.java`),继承自 `JpaRepository` 或具体的 Entity 接口,例如: ```java @Entity public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // ...字段和getter/setter方法 } interface MyRepository extends JpaRepository<MyEntity, Long> {} ``` 5. **配置扫描包**: 在主类上添加 `@EnableJpaRepositories` 注解,指定扫描 Repository 接口的位置: ```java @SpringBootApplication @EnableJpaRepositories(basePackages = "com.example.myapp.repository") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 6. **运行应用**: 使用构建工具构建并运行项目,Spring Boot 将自动配置 JPA 并创建数据库表。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值