springboot 报错 org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.example.service.HrService' avai
话不多说先上图,这是启动类的配置,这里配置了@componentscan("我的mapper的接口")
接下来是我的项目结构截图
然后是service 的截图,我在这里加了注解@service
最后我在测试类里面 的截图
最后附上我的maven的pom 我开始怀疑是这个配置的有问题了
<?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.2.2.release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>
<groupid>com.example</groupid>
<artifactid>demo</artifactid>
<version>0.0.1-snapshot</version>
<name>demo</name>
<description>demo project for spring boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
<version>1.16.10</version>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-cache</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
<exclusions>
<exclusion>
<groupid>io.lettuce</groupid>
<artifactid>lettuce-core</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
</dependency>
<dependency>
<groupid>com.alibaba</groupid>
<artifactid>druid</artifactid>
<version>1.1.10</version>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-security</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupid>org.mybatis.spring.boot</groupid>
<artifactid>mybatis-spring-boot-starter</artifactid>
<version>2.1.1</version>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<scope>runtime</scope>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
<exclusions>
<exclusion>
<groupid>org.junit.vintage</groupid>
<artifactid>junit-vintage-engine</artifactid>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupid>io.projectreactor</groupid>
<artifactid>reactor-test</artifactid>
<scope>test</scope>
</dependency>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
详细的报错信息如下:
caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'com.example.service.hrservice' available: expected at least 1 bean which qualifies as autowire candidate. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
at org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenomatchingbeanfound(defaultlistablebeanfactory.java:1695) ~[spring-beans-5.2.2.release.jar:5.2.2.release]
at org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1253) ~[spring-beans-5.2.2.release.jar:5.2.2.release]
at org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1207) ~[spring-beans-5.2.2.release.jar:5.2.2.release]
at org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:640) ~[spring-beans-5.2.2.release.jar:5.2.2.release]
经过几位大佬的提醒,在启动类新增了@mapperscan("com.example.mappar"),同时在测试类加入启动类@runwith(springrunner.class) 解决了找不dao bean 的问题,但随后又产生了一个新的报错如下:
与之对应的我的mapper.xml文件如下:
我的mapper接口如下:
最后我的hrservice:
详细报错如下:
我在网上翻阅了资料:有的解决办法是看pom.xml里面的build 下 resouce 没有添加,我添加了后发现,还是无效。此外在namespace 和对应的service 里面,mapper 方法的名称都是一致,在torget目录下,我的hrmappar.class 和hrservice.xml在同一目录下。很疑惑问题出在哪里了。