Home > other >  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 

Time:11-24

I'm getting this error when trying to start my Spring application. My classes are attached below but in a simplified manner.

The projet uses the pattern dto → service → serviceImpl → repository.

DentistDto.java

@Data
public class DentistDto {

    @NotBlank
    @Size(max = 11)
    private String croNumber;

    @Valid
    Person person;

}

DentistModel.java

@Data
@Entity
@Table(name = "dentists")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class DentistModel {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "dentist_id", updatable = false, nullable = false)
    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID id;

    @Column(nullable = false, unique = true, length = 10)
    private String croNumber;

...

DentistController.java

    @CrossOrigin
    @RequestMapping("/dentists")
    @RestController
    public class DentistController {
    
         private final DentistService dentistService;
    
        public DentistController(DentistService dentistService) {
            this.dentistService = dentistService;
        }
    
        @PostMapping
        public ResponseEntity<Object> saveDentist(@RequestBody @Valid DentistDto dentistDto) {
            return dentistService.save(dentistDto);
        }
    
        @GetMapping
        public ResponseEntity<Object> getAllDentists() throws NotFoundException {
            return dentistService.findAll();
        }
...

DentistServiceImpl.java

    @Service
    public class DentistServiceImpl implements DentistService {
    
        private final DentistRepository dentistRepository;
        private final DentistMapper dentistMapper = DentistMapper.INSTANCE;
    
        public DentistServiceImpl(DentistRepository dentistRepository) {
            this.dentistRepository = dentistRepository;
        }
    
        @Override
        public DentistDto findById(UUID id) throws NotFoundException {
            var dentist = dentistRepository.findById(id).orElseThrow(() -> new NotFoundException());
            return dentistMapper.toDto(dentist);
        }
...

DentistService.java

public interface DentistService {

    DentistDto findById(UUID id) throws NotFoundException;

    ResponseEntity<Object> findAll() throws NotFoundException;

    ResponseEntity<Object> save(DentistDto dto);

...

DentistRepository.java

@Repository
public interface DentistRepository extends JpaRepository<DentistModel, UUID> {

    Optional<Object> findByCroNumber(String croNumber);
}

DentistMapper.java

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

    @Mapper
    public interface DentistMapper {
    
        DentistMapper INSTANCE = Mappers.getMapper(DentistMapper.class);
    
        DentistModel toModel(DentistDto dto);
        DentistDto toDto(DentistModel model);
    }

Full error

24-11-2022 09:46:41.159 | 178 | [main] | INFO | com.api.lores.LoresApplication - Starting the Lores API 24-11-2022 09:46:41.312 | 331 | [Thread-0] | DEBUG | o.s.b.d.r.c.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@4fb091b 24-11-2022 09:46:41.315 | 334 | [restartedMain] | INFO | com.api.lores.LoresApplication - Starting the Lores API 24-11-2022 09:46:41.605 | 624 | [restartedMain] | INFO | com.api.lores.LoresApplication - Starting LoresApplication using Java 17.0.4.1 on DESKTOP-733E7TU with PID 13984 (C:\Users\Guilherme Lopes\repos\lores\target\classes started by Guilherme Lopes in C:\Users\Guilherme Lopes\repos\lores) 24-11-2022 09:46:41.606 | 625 | [restartedMain] | INFO | com.api.lores.LoresApplication - No active profile set, falling back to 1 default profile: "default" 24-11-2022 09:46:44.565 | 3584 | [restartedMain] | WARN | o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dentistController' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\controller\DentistController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError 24-11-2022 09:46:44.599 | 3618 | [restartedMain] | ERROR | o.s.boot.SpringApplication - Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dentistController' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\controller\DentistController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.api.lores.LoresApplication.main(LoresApplication.java:17) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dentistServiceImpl' defined in file [C:\Users\Guilherme Lopes\repos\lores\target\classes\com\api\lores\service\dentist\DentistServiceImpl.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:296) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ... 24 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.api.lores.service.dentist.DentistServiceImpl]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:311) ... 38 common frames omitted Caused by: java.lang.ExceptionInInitializerError: null at com.api.lores.service.dentist.DentistServiceImpl.(DentistServiceImpl.java:23) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211) ... 40 common frames omitted Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:61) at com.api.lores.mapper.DentistMapper.(DentistMapper.java:10) ... 47 common frames omitted Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75) at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) ... 48 common frames omitted

How can I fix this type of error?

CodePudding user response:

Uh, that stacktrace is badly mangled. It looks like you copied this from a console window with low line width that added plenty of line breaks, then used stack overflow's quote feature to eat all linebreaks. You can obtain a less mangled stack trace by logging to a file rather than a console, or using a console with much larger line width.

Having done that, the stack trace clearly tells you:

Caused by: java.lang.ClassNotFoundException: Cannot find implementation for com.api.lores.mapper.DentistMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58) 
    ... 48 common frames omitted

(in Java, the root cause of an error is usually the last exception in the stack trace)

Now that you know which exception to google, the answer should be much closer at hand :-)

CodePudding user response:

Try to add @Service to your DentistService, Like this:

@Service
public interface DentistService {

    DentistDto findById(UUID id) throws NotFoundException;

    ResponseEntity<Object> findAll() throws NotFoundException;

    ResponseEntity<Object> save(DentistDto dto);

}
  • Related