Home > Enterprise >  Spring can't find implementation
Spring can't find implementation

Time:12-10

Here is my folder structure:

enter image description here

In my IAppUserMapper I have a method to convert every AppUser entity instance to Data Transfer Object Model. Here is the code in IAppUserMapper interface:


import com.server.ecommerceapp.dto.AppUserDTO;
import com.server.ecommerceapp.model.AppUser;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@Mapper
public interface IAppUserMapper {

    IAppUserMapper appUserMapper = Mappers.getMapper(IAppUserMapper.class);

    @Mapping(target = "username")
    @Mapping(target = "email")
    @Mapping(target = "password")
     AppUserDTO toAppUserDTO(AppUser appUser);
}  


And here is the MapperConfiguration class code where I configure both Product and user mappers:


import com.server.ecommerceapp.mapper.IAppUserMapper;
import com.server.ecommerceapp.mapper.IProductMapper;
import org.mapstruct.factory.Mappers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MapperConfiguration {
    @Bean
    public IAppUserMapper appUserMapper() {
        return Mappers.getMapper(IAppUserMapper.class);
    }

    @Bean
    public IProductMapper productMapper() {
        return Mappers.getMapper(IProductMapper.class);
    }
}

The error I get:

Error creating bean with name 'appUserMapper' defined in class path resource [com/server/ecommerceapp/configuration/MapperConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.server.ecommerceapp.mapper.IAppUserMapper]: Factory method 'appUserMapper' threw exception; nested exception is java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.server.ecommerceapp.mapper.IAppUserMapper

I was told I should make META-INF package in resources, with service package and the com.server.ecommerceapp.mapper.AppUserMapper txt with the content same as the name of the file, so that Spring can scan and find the package following the path:

src/main/resources/META-INF/service/com.server.ecommerceapp.mapper.AppUserMapper

but it didnt work. Any ideas how to solve this, and by the way, is it bad practise to start interface names with capital I cause Im coming from ASP?

CodePudding user response:

The file name of the service should be the interface and its content the implementation. You have named it by the implementation.

CodePudding user response:

So you're using Spring, but you are trying to not use Spring.

You should make your mappers use Spring component model:

@Mapper(componentModel = "spring")
public interface MyMapper {
  Target map(Source source);
}

Check docs for dependency injection: https://mapstruct.org/documentation/stable/reference/html/#using-dependency-injection

Or do it with shared configuration: https://mapstruct.org/documentation/stable/reference/html/#shared-configurations

After that you can just @Autowired MyMapper myMapper; as any other Spring bean. No need to create instance in interface (the "Mappers.getMapper" thing) and no need to create mappers in java configuration, cause they bean creating will be handled by framework.

  • Related