springboot 没法扫描到repository 求大神帮忙看看

remin 发布于 06/08 17:00
阅读 1K+
收藏 0

 启动报这个错误

Description:

A component required a bean of type 'me.zhongmin.zero.dao.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'me.zhongmin.zero.dao.UserRepository' in your configuration.

下面是配置文件:

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- springboot测试模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 使用spring的Aspectj需要引入aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!--websocket-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        
        <dependency>
		    <groupId>com.google.guava</groupId>
		    <artifactId>guava</artifactId>
		    <version>25.0-jre</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
		<dependency>
		    <groupId>org.apache.kafka</groupId>
		    <artifactId>kafka-clients</artifactId>
		    <version>0.10.2.0</version>
		</dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <!--数据库相关依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!--quartz相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

        <!-- 添加对jsp视图解析的支持 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- 下面两个引入为了操作数据库 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- Json包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <!--   <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-ribbon</artifactId>
           </dependency>-->
    </dependencies>

启动类

目录结构:其他包下面的类都可以扫描到但是扫描不到dao包下面的userRepository

userRepository.java

 

搜索了半天说是需要在启动类上加enableRepository注解

加完了之后又出现这个错误

Description:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

求大神帮忙看看哪里有问题呢

加载中
0
傲_冰
傲_冰

DataSourceAutoConfiguration为什么要排除掉,里面有一些数据源初始化的配置

0
OSC_foNCgp
OSC_foNCgp
看看我这篇博客,有你的答案http://yangyitao.top/blog/2018/06/05/SpringBootJpa01/
remin
remin
你好 我的spirngboot是2.0的版本 应该如何注入呢
0
OSC_foNCgp
OSC_foNCgp
你把自动配置去掉了,提示缺少entityManagerFactory这个bean,可以点一下源码,根据源码手动自己写一个这个bean,bean名字一样就行
remin
remin
是因为排除了DataSourceAutoConfiguration这个自动配置吗
remin
remin
兄弟 你看的是什么教材啊 用的2.0吗
0
OSC_foNCgp
OSC_foNCgp
李家智的springBoot2.0精髓
0
Caishu
Caishu

把@SpringBootApplication注解中的exclude = DataSourceAutoConfiguration.class去掉,然后在application.properties配置文件中加上spring.datasourc的相关配置就可以了。我之前也遇到跟你一样的问题,主要exclude = DataSourceAutoConfiguration.class是这个导致的,走了很多弯路比如注解扫描repository包不行,因为这样会扫描不到controller包

返回顶部
顶部