okinawa

IT勉強メモ

Spring Boot起動時エラー field in required a bean of type that could not be found

同じ状況での解決方法が、ネット上に見つからなかったのでメモ。

現象

Spring Boot起動時に出たエラー。

*************************
APPLICATION FAILED START
*************************
Description:

Field 'MapperInterface名' in 'Serviceクラス名' required a bean of type 'MapperInterface名' 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 'MapperInterface名' in your configuration.

Serviceクラスの中で使っているMapperInterfaceが見つかりません。

AutowiredでMapperInterfaceを呼び出してますよ。

設定に 'MapperInterface 名' 型の Bean を定義することを検討してください、的なことを言っている。

要するにMapperInterfaceがDIされてないよ、ということ。

状況

SpringBoot2x→3xにバージョンアップ後に起きた。

Mybatisを使っていて、MapperInterfaceに@Mapperが付いていた。

@Mapper
public interface Mapper {  }

@MapperついてたらDIされるのでは???とだいぶ混乱した。

原因

MyBatis Spring Boot Starterのバージョンが古かった

pom.xmlを編集。1.0.2→3.0.3

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

SpringBootの基本的なことがわかってなくて大変だった。

お勉強します。