Spring Boot错误:Consider defining a bean of type '*.*.Dao' in your configuration

SpringBoot在启动项目的时候遇到了以下情况:

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

Description:

Field sysUserDao in com.iamapsycho.service.impl.SysUserServiceImpl required a bean of type 'com.iamapsycho.dao.SysUserDao' that could not be found.


Action:

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

SpringBoot启动失败,告诉我Bean配置失败,为什么报错呢?

Controller:  

 package com.iamapsycho.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.iamapsycho.entity.SysUser;
    import com.iamapsycho.service.SysUserService;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    
    @Controller
    @Api(value = "sysUser接口")
    @RequestMapping("/sysuser")
    public class SysUserController {
        
        @Autowired
        SysUserService sysUserService;
        
        @ResponseBody
        @RequestMapping(value="/getList", method = { RequestMethod.GET, RequestMethod.POST })
        @ApiOperation(value="获取用户列表", notes="用户列表")    
        public List<SysUser> getList(){
            List<SysUser> list = sysUserService.getList();
            return list;
        }
    }

Service:

  package com.iamapsycho.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.iamapsycho.dao.SysUserDao;
    import com.iamapsycho.entity.SysUser;
    import com.iamapsycho.service.SysUserService;
    
    @Service
    public class SysUserServiceImpl implements SysUserService {
    
        @Autowired
        SysUserDao sysUserDao;
        
        @Override
        public List<SysUser> getList() {
            return sysUserDao.getList();
        }
    
    }

Dao:

  package com.iamapsycho.dao;
    
    import java.util.List;
    
    import com.iamapsycho.entity.SysUser;
    
    public interface SysUserDao {
    
        List<SysUser> getList();
    
    }

在网上看到网友说要用@Mapper注解,这才把问题解决了 ,至于具体原因,需要通过文档来解释。

解决方案一:
Dao层
添加:@Mapper

  package com.iamapsycho.dao;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    
    import com.iamapsycho.entity.SysUser;
    
    @Mapper
    public interface SysUserDao {
    
        List<SysUser> getList();
    
    }

解决方案二(强烈建议使用):
Application(启动类)
添加:@MapperScan(value = “com.iamapsycho.dao”)

    package com.iamapsycho;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan(value = "com.iamapsycho.dao")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }


作者:iamapsycho 
来源:CSDN 
原文:https://blog.csdn.net/ampsycho/article/details/86243817 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
To define a bean of type 'redis.RedisUtil' in your configuration, you can follow these steps: 1. Make sure you have the necessary dependencies added to your project. Typically, for Redis-related operations, you will need the 'spring-boot-starter-data-redis' dependency. 2. Create a RedisUtil class that provides the necessary operations and functionalities related to Redis. This class should have methods for connecting to Redis, performing CRUD operations, and any other Redis-specific operations you require. 3. Annotate the RedisUtil class with the '@Component' annotation to make it a Spring bean. This annotation tells Spring to manage the lifecycle of this class and make it available for autowiring. 4. In your application configuration class (usually annotated with '@SpringBootApplication'), use the '@ComponentScan' annotation to scan for the RedisUtil class. This ensures that Spring discovers the RedisUtil bean and makes it available for injection. Here is an example: ```java import org.springframework.stereotype.Component; @Component public class RedisUtil { // Your RedisUtil implementation here } ``` ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.yourpackage") public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` Make sure to replace "com.yourpackage" with the actual package where your RedisUtil class resides. With these steps, you should be able to define a bean of type 'redis.RedisUtil' in your configuration.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值