I'm working on a Spring Boot application and I'm trying to inject a SourcingMapper
bean into my SourcingServiceImpl
class using the @Autowired
annotation, but I keep getting an error when I try to start the application:
Field sourcingMapper in com.immobilia.immobiliaapp.service.SourcingServiceImpl required a bean of type 'com.immobilia.immobiliaapp.mapper.SourcingMapper' that could not be found.
My MapStruct Interface:
@Mapper(componentModel = "spring")
@Component
public interface SourcingMapper {
SourcingDto toSourcingDto(SourcingController sourcing);
SourcingController toSourcing(SourcingDto sourcingDto);
List<SourcingDto> toSourcingDto(List<SourcingController> sourcings);
List<SourcingController> toSourcing(List<SourcingDto> sourcingDtos);
}
My Service:
@Service
public class SourcingServiceImpl implements SourcingService {
@Autowired
SourcingRepo sourcingRepo;
@Autowired
SourcingMapper sourcingMapper;
@Override
public SourcingDto addSourcing(SourcingDto sourcing) {
return sourcingRepo.save(sourcing);
}
}
My pom.xml
<?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>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.immobilia</groupId>
<artifactId>immobilia-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>immobilia-app</name>
<description>immobilia-app</description>
<properties>
<java.version>19</java.version>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>