布熬夜了 2023-02-25 16:28 采纳率: 0%
浏览 116
已结题

新建的SpringBoot项目无法扫描到Dao层

新建的SpringBoot项目无法扫描到Dao层,
控制台输出信息如下:

/Library/Java/JavaVirtualMachines/jdk-11.0.17.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:63596,suspend=y,server=n -javaagent:/Users/zjwu/Library/Caches/JetBrains/IdeaIC2022.2/captureAgent/debugger-agent.jar -Dfile.encoding=UTF-8 -classpath /Users/zjwu/Documents/personal/backend/catalpa-authorization-backend/target/classes:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-starter/2.5.12/spring-boot-starter-2.5.12.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot/2.5.12/spring-boot-2.5.12.jar:/Users/zjwu/.m2/repository/org/springframework/spring-context/5.3.18/spring-context-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.5.12/spring-boot-autoconfigure-2.5.12.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.5.12/spring-boot-starter-logging-2.5.12.jar:/Users/zjwu/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar:/Users/zjwu/.m2/repository/ch/qos/logback/logback-core/1.2.11/logback-core-1.2.11.jar:/Users/zjwu/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.17.2/log4j-to-slf4j-2.17.2.jar:/Users/zjwu/.m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar:/Users/zjwu/.m2/repository/org/slf4j/jul-to-slf4j/1.7.36/jul-to-slf4j-1.7.36.jar:/Users/zjwu/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/zjwu/.m2/repository/org/springframework/spring-core/5.3.18/spring-core-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/spring-jcl/5.3.18/spring-jcl-5.3.18.jar:/Users/zjwu/.m2/repository/org/yaml/snakeyaml/1.28/snakeyaml-1.28.jar:/Users/zjwu/.m2/repository/net/bytebuddy/byte-buddy/1.10.22/byte-buddy-1.10.22.jar:/Users/zjwu/.m2/repository/org/springframework/spring-web/5.3.18/spring-web-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/spring-beans/5.3.18/spring-beans-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.5.12/spring-boot-starter-web-2.5.12.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.5.12/spring-boot-starter-json-2.5.12.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.6.1/jackson-databind-2.12.6.1.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.6/jackson-core-2.12.6.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.12.6/jackson-datatype-jdk8-2.12.6.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.12.6/jackson-datatype-jsr310-2.12.6.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.12.6/jackson-module-parameter-names-2.12.6.jar:/Users/zjwu/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.5.12/spring-boot-starter-tomcat-2.5.12.jar:/Users/zjwu/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.60/tomcat-embed-core-9.0.60.jar:/Users/zjwu/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.60/tomcat-embed-el-9.0.60.jar:/Users/zjwu/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.60/tomcat-embed-websocket-9.0.60.jar:/Users/zjwu/.m2/repository/org/springframework/spring-webmvc/5.3.18/spring-webmvc-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/spring-aop/5.3.18/spring-aop-5.3.18.jar:/Users/zjwu/.m2/repository/org/springframework/spring-expression/5.3.18/spring-expression-5.3.18.jar:/Users/zjwu/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.5/mybatis-generator-core-1.3.5.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-swagger2/2.9.2/springfox-swagger2-2.9.2.jar:/Users/zjwu/.m2/repository/io/swagger/swagger-annotations/1.5.20/swagger-annotations-1.5.20.jar:/Users/zjwu/.m2/repository/io/swagger/swagger-models/1.5.20/swagger-models-1.5.20.jar:/Users/zjwu/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.6/jackson-annotations-2.12.6.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-spi/2.9.2/springfox-spi-2.9.2.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-core/2.9.2/springfox-core-2.9.2.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-schema/2.9.2/springfox-schema-2.9.2.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-swagger-common/2.9.2/springfox-swagger-common-2.9.2.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-spring-web/2.9.2/springfox-spring-web-2.9.2.jar:/Users/zjwu/.m2/repository/com/google/guava/guava/20.0/guava-20.0.jar:/Users/zjwu/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar:/Users/zjwu/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar:/Users/zjwu/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar:/Users/zjwu/.m2/repository/org/springframework/plugin/spring-plugin-metadata/1.2.0.RELEASE/spring-plugin-metadata-1.2.0.RELEASE.jar:/Users/zjwu/.m2/repository/org/mapstruct/mapstruct/1.2.0.Final/mapstruct-1.2.0.Final.jar:/Users/zjwu/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.jar:/Users/zjwu/.m2/repository/com/alibaba/fastjson/2.0.8.graal/fastjson-2.0.8.graal.jar:/Users/zjwu/.m2/repository/com/alibaba/fastjson2/fastjson2-extension/2.0.8.graal/fastjson2-extension-2.0.8.graal.jar:/Users/zjwu/.m2/repository/com/alibaba/fastjson2/fastjson2/2.0.8.graal/fastjson2-2.0.8.graal.jar:/Users/zjwu/.m2/repository/org/mybatis/mybatis/3.5.10/mybatis-3.5.10.jar:/Users/zjwu/.m2/repository/org/springframework/spring-tx/5.2.15.RELEASE/spring-tx-5.2.15.RELEASE.jar:/Users/zjwu/.m2/repository/com/github/pagehelper/pagehelper/5.3.0/pagehelper-5.3.0.jar:/Users/zjwu/.m2/repository/com/github/jsqlparser/jsqlparser/4.2/jsqlparser-4.2.jar:/Users/zjwu/.m2/repository/joda-time/joda-time/2.10/joda-time-2.10.jar:/Users/zjwu/.m2/repository/javax/persistence/javax.persistence-api/2.2/javax.persistence-api-2.2.jar:/Users/zjwu/.m2/repository/tk/mybatis/mapper-core/1.1.5/mapper-core-1.1.5.jar:/Users/zjwu/.m2/repository/javax/persistence/persistence-api/1.0/persistence-api-1.0.jar:/Users/zjwu/.m2/repository/tk/mybatis/mapper-extra/1.1.5/mapper-extra-1.1.5.jar:/Users/zjwu/.m2/repository/tk/mybatis/mapper-base/1.1.5/mapper-base-1.1.5.jar:/Users/zjwu/.m2/repository/org/mybatis/mybatis-spring/2.0.7/mybatis-spring-2.0.7.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar com.catalpa.authorization.AuthorizationApplication
已连接到目标 VM, 地址: ''127.0.0.1:63596',传输: '套接字''

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.5.12)

2023-02-25 16:22:14.388  INFO 12842 --- [           main] c.c.a.AuthorizationApplication           : Starting AuthorizationApplication using Java 11.0.17 on MacBook-Pro-2.local with PID 12842 (/Users/zjwu/Documents/personal/backend/catalpa-authorization-backend/target/classes started by zjwu in /Users/zjwu/Documents/personal/backend/catalpa-authorization-backend)
2023-02-25 16:22:14.392  INFO 12842 --- [           main] c.c.a.AuthorizationApplication           : The following 1 profile is active: "default"
2023-02-25 16:22:15.390  INFO 12842 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8094 (http)
2023-02-25 16:22:15.400  INFO 12842 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-02-25 16:22:15.401  INFO 12842 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
2023-02-25 16:22:15.497  INFO 12842 --- [           main] o.a.c.c.C.[.[.[/authorization-default]   : Initializing Spring embedded WebApplicationContext
2023-02-25 16:22:15.497  INFO 12842 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1040 ms
2023-02-25 16:22:15.570  WARN 12842 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysUserImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.catalpa.authorization.dao.SysUserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup="", name="", description="", authenticationType=CONTAINER, type=java.lang.Object.class, mappedName="")}
2023-02-25 16:22:15.573  INFO 12842 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-02-25 16:22:15.584  INFO 12842 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-02-25 16:22:15.600 ERROR 12842 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

A component required a bean of type 'com.catalpa.authorization.dao.SysUserDao' that could not be found.


Action:

Consider defining a bean of type 'com.catalpa.authorization.dao.SysUserDao' in your configuration.

与目标 VM 断开连接, 地址为: ''127.0.0.1:63596',传输: '套接字''

进程已结束,退出代码1

application代码如下:

package com.catalpa.authorization;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author zjwu
 */
@ComponentScan(basePackages = {
        "com.catalpa.authorization.**"
})
@SpringBootApplication
public class AuthorizationApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AuthorizationApplication.class);
    }

    public static void main(String[] args) {
        new AuthorizationApplication().configure(new SpringApplicationBuilder(AuthorizationApplication.class)).run(args);
    }

}

controller类如下:

package com.catalpa.authorization.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zjwu
 */
@RestController
@RequestMapping(value = "/service/dept")
@Api(tags = "测试")
public class SysDeptController {

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    public String getTest() {
        return "hello world";
    }
}


service层如下:

package com.catalpa.authorization.service;

import com.catalpa.authorization.bean.SysUser;
import com.catalpa.authorization.utils.IBaseService;

/**
 * @author zjwu
 */
public interface ISysUserService extends IBaseService<SysUser> {
}


package com.catalpa.authorization.service.impl;

import com.catalpa.authorization.bean.SysUser;
import com.catalpa.authorization.dao.SysUserDao;
import com.catalpa.authorization.service.ISysUserService;
import com.catalpa.authorization.utils.BaseMapper;
import com.catalpa.authorization.utils.BaseServiceImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zjwu
 */
@Repository
@Transactional(rollbackFor = Exception.class)
public class SysUserImpl extends BaseServiceImpl<SysUser> implements ISysUserService {
    @Resource
    private SysUserDao sysUserDao;

    @Override
    public BaseMapper<SysUser> getDao() {
        return this.sysUserDao;
    }
}


Dao层如下:

package com.catalpa.authorization.dao;

import com.catalpa.authorization.bean.SysUser;
import com.catalpa.authorization.utils.BaseMapper;

/**
 * @author zjwu
 */
public interface SysUserDao extends BaseMapper<SysUser> {
}


  • 写回答

12条回答 默认 最新

  • GPT_Pro 2023-02-25 16:37
    关注

    该回答内容部分引用GPT,GPT_Pro更好的解决问题
    新建的SpringBoot项目无法扫描到Dao层,这说明你的dao层没有被SpringBoot扫描到,可能是你的配置文件有问题。

    一般来说,SpringBoot会自动扫描所有在启动类package(或者其子package)下的bean(包括dao层),如果没有被扫描到,那么可能是你的启动类package设置有问题。

    你可以尝试手动让SpringBoot来扫描dao层,方法是在你的启动类上添加@ComponentScan注解:

    @ComponentScan(basePackages = {"com.xxx.xxx.dao"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }
    

    这里使用basePackages参数来定义扫描的package,这样就可以将你定义的dao层包加入到Spring Boot的扫描范围内。

    此外,如果你想要实现对数据库的访问,需要在pom.xml文件中添加相应数据库驱动依赖:

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

    这里以Mysql为例,此外还有其他数据库的驱动依赖。当然你也可以通过application.properties文件来进行数据库连接配置:

    spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    spring.datasource.username=root
    spring.datasource.password=root123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
    

    最后,还需要在Application类上标注@EnableTransactionManagement注解来开启事务管理。带上这些配置之后就可以正常使用dao层了。
    如果回答有帮助,望采纳。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月4日
  • 创建了问题 2月25日

悬赏问题

  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?