APPLICATION FAILED TO START
Parameter 1 of constructor in Kodlama.io.Devs.business.concretes.ProgrammingLanguageManager required a bean of type 'org.modelmapper.ModelMapper' that could not be found.
Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration.
ProgrammingLanguageManager
@Service
public class ProgrammingLanguageManager implements ProgrammingLanguageService {
@Autowired
private ProgrammingLanguageRepository programmingLanguageRepository;
private ModelMapper modelMapper;
@Autowired
public ProgrammingLanguageManager(ProgrammingLanguageRepository programmingLanguageRepository,
ModelMapper modelMapper) {
this.programmingLanguageRepository = programmingLanguageRepository;
this.modelMapper = modelMapper;
}
// * * * CRUD OPERATIONS * * *
@Override
public CreateProgrammingLanguageResponse add(CreateProgrammingLanguageRequest createProgrammingLanguageRequest)
throws Exception {
nameCannotBeSame(createProgrammingLanguageRequest.getName());
ProgrammingLanguage programmingLanguage = modelMapper.map(createProgrammingLanguageRequest,
ProgrammingLanguage.class);
ProgrammingLanguage saveProgrammingLanguageResult = programmingLanguageRepository.save(programmingLanguage);
CreateProgrammingLanguageResponse createProgrammingLanguageResponse = modelMapper
.map(saveProgrammingLanguageResult, CreateProgrammingLanguageResponse.class);
return createProgrammingLanguageResponse;
}
@Override
public UpdateProgrammingLanguageResponse update(UpdateProgrammingLanguageRequest updateProgrammingLanguageRequest)
throws Exception {
nameCannotBeSame(updateProgrammingLanguageRequest.getName());
ProgrammingLanguage programmingLanguage = modelMapper.map(updateProgrammingLanguageRequest,
ProgrammingLanguage.class);
ProgrammingLanguage saveProgrammingLanguageResult = programmingLanguageRepository.save(programmingLanguage);
UpdateProgrammingLanguageResponse updateProgrammingLanguageResponse = modelMapper
.map(saveProgrammingLanguageResult, UpdateProgrammingLanguageResponse.class);
return updateProgrammingLanguageResponse;
}
@Override
public DeleteProgrammingLanguageResponse delete(DeleteProgrammingLanguageRequest deleteProgrammingLanguageRequest) {
ProgrammingLanguage getReferenceByIdProgrammingLanguageResult = programmingLanguageRepository
.getReferenceById(deleteProgrammingLanguageRequest.getId());
programmingLanguageRepository.delete(getReferenceByIdProgrammingLanguageResult);
DeleteProgrammingLanguageResponse deleteProgrammingLanguageResponse = modelMapper
.map(getReferenceByIdProgrammingLanguageResult, DeleteProgrammingLanguageResponse.class);
return deleteProgrammingLanguageResponse;
}
// * * * GET METHODS * * *
@Override
public List<GetAllProgrammingLanguagesResponse> getAll() {
List<ProgrammingLanguage> findAllProgrammingLanguageResult = programmingLanguageRepository.findAll();
return modelMapper.map(findAllProgrammingLanguageResult,
new TypeToken<List<GetAllProgrammingLanguagesResponse>>() {
}.getType());
}
@Override
public GetByIdProgrammingLanguageResponse getById(
GetByIdProgrammingLanguageRequest getByIdProgrammingLanguageRequest) {
ProgrammingLanguage getReferenceByIdProgrammingLanguageResult = programmingLanguageRepository
.getReferenceById(getByIdProgrammingLanguageRequest.getId());
GetByIdProgrammingLanguageResponse getByIdProgrammingLanguageResponse = modelMapper
.map(getReferenceByIdProgrammingLanguageResult, GetByIdProgrammingLanguageResponse.class);
return getByIdProgrammingLanguageResponse;
}
public GetByNameProgrammingLanguageResponse getByName(
GetByNameProgrammingLanguageRequest getByNameProgrammingLanguageRequest) {
ProgrammingLanguage getReferenceByNameProgrammingLanguageResult = new ProgrammingLanguage();
getReferenceByNameProgrammingLanguageResult = programmingLanguageRepository
.getByName(getByNameProgrammingLanguageRequest.toString());
GetByNameProgrammingLanguageResponse getByNameProgrammingLanguageResponse = modelMapper
.map(getReferenceByNameProgrammingLanguageResult, GetByNameProgrammingLanguageResponse.class);
return getByNameProgrammingLanguageResponse;
}
// * * * BUSINESS RULES * * *
private void nameCannotBeSame(String name) throws Exception {
ProgrammingLanguage programmingLanguage = programmingLanguageRepository.getByName(name);
var result = programmingLanguage;
if (result != null) {
throw new Exception(Messages.NAME_ALREADY_EXISTS);
}
}
}
Im missing something but i don't know what
Your ModelMapper class is not registered as spring bean, it is just a normal java class.
In order to fix that, you need to create a @Configuration
class that will define the bean if you'd like to autowire it.
@Configuration
public class AppConfiguration {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Otherwise, you cannot use spring dependency injection and you would have to create the object yourself with new ModelMapper();
Another tip is to avoid annotating fields with @Autowire
. Constructor level annotation is enough.