Springboot 3.2.2使用@Autowire注入bean失败,但将springboot版本更改回2.7.5时可以工作

问题描述 投票:0回答:2

你好,

当我尝试启动我的主 springboot 类时,出现此错误。

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

Description:

Field deptMapper in com.itheima.service.impl.DeptServiceImpl required a bean of type 'com.itheima.mapper.DeptMapper' 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.itheima.mapper.DeptMapper' in your configuration.

我使用的 springboot 版本是 3.2.2

但是,当我在 pom.xml 中将其更改回 2.7.5 时,它可以正常启动应用程序。使用两个版本的 springboot 时,我的代码保持不变。

我就是不明白为什么。 我的文件结构很好,我按照 springboots 的路径定义说明进行操作。如果这有帮助的话,这是我的路径的图片: project structure

任何相关的信息都将不胜感激,谢谢!!! 以下是我的代码:

控制器java文件:

package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController //
public class DeptController {
    @Autowired(required = true)
    private DeptService deptService;
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list() {
        log.info("search all departments");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
}


服务层(DAO)接口文件:

package com.itheima.service;

import com.itheima.pojo.Dept;

import java.util.List;

public interface DeptService {
    /*search all departments*/
    List<Dept> list();
}

服务层实现类(DAO)

package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service //当前实现类交给IOC容器管理,成为容器的bean对象
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
}

使用mybatis从mysql获取数据的Mapper接口:

package com.itheima.mapper;

import com.itheima.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DeptMapper {
    //    查询全部的部门数据
    @Select("select * from dept")
    List<Dept> list();
}

IOC容器注入数据的Pojo类:

package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    private Integer id;
    private String name;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}

当我在 pom.xml 中将 springboot 版本更改回 2.7.5 时,它可以正常启动应用程序。使用两个版本的 springboot 时,我的代码保持不变。我就是不明白为什么。

java spring-boot inversion-of-control mybatis spring-boot-3
2个回答
0
投票

尝试

@Mapper(componentModel = ComponentModel.SPRING)
。这对我有用。


-1
投票

在新版本的 Spring boot 中可以通过构造函数使用自动装配:

@Service //当前实现类交给IOC容器管理,成为容器的bean对象
public class DeptServiceImpl implements DeptService {
    private **final** DeptMapper deptMapper;

    DeptServiceImpl (DeptMapper deptMapper){
      this.deptMapper = deptMapper;
    }
© www.soinside.com 2019 - 2024. All rights reserved.