728x90
반응형
프로그램이 실행이 되질 않는 문제 발생 !
에러 코드
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'simpleProductService' defined in file [/Users/soheepark/Downloads/product.management/target/classes/kr/co/hanbit/product/management/application/SimpleProductService.class]: Unsatisfied dependency expressed through constructor parameter 1: Error creating bean with name 'modelMapper' defined in kr.co.hanbit.product.management.Application: Failed to instantiate [org.modelmapper.ModelMapper]: Factory method 'modelMapper' threw exception with message: null
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'modelMapper' defined in kr.co.hanbit.product.management.Application: Failed to instantiate [org.modelmapper.ModelMapper]: Factory method 'modelMapper' threw exception with message: null
- BeanCreationException : 스프링 컨테이너가 빈을 생성하는 과정에서 문제가 발생했다는 의미.
modelMapper() 메소드 호출 부분에서 잘못된 호출이 있었다.
현재 modelMapper()를 Bean으로 등록하는 과정에서 호출이 잘못되어 Bean 생성에 실패하고 있는 것이 원인.
다음은 modelMapper() 메소드를 잘못 호출한 코드 전문이다.
package kr.co.hanbit.product.management;
import org.modelmapper.ModelMapper;
import org.modelmapper.config.Configuration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper= new ModelMapper();
modelMapper.getConfiguration()
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE)
.setFieldMatchingEnabled(true);
return modelMapper(); // 해당 부분에서 잘못된 호출이 이루어졌음.
}
}
return modelMapper();
modelMapper 메서드를 다시 호출하려고 시도하게 되는 코드이다.
이 코드가 문제였던 것임.
그렇다면 modelMapper를 정상적으로 호출하려면 어느 부분을 변경해야 할까 ?
return문을 수정하면 된다.
modelMapper 인스턴스를 반환하는 코드로 변경하면 된다.
modelMapper의 Bean 생성 문제를 해결하기 위해 코드를 수정했다.
package kr.co.hanbit.product.management;
import org.modelmapper.ModelMapper;
import org.modelmapper.config.Configuration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper= new ModelMapper();
modelMapper.getConfiguration()
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE)
.setFieldMatchingEnabled(true);
return modelMapper; // 수정 완료.
}
}
다시 애플리케이션 실행 !
실행 잘 됐다 !
Postman도 실행 잘 된다 !
728x90
반응형
'[Language] > Java | Spring | JSP' 카테고리의 다른 글
Local variable 'savedProductDto' is redundant : 해당 변수 불필요 (0) | 2024.05.27 |
---|---|
Field 'modelMapper' may be 'final' : final 선언 (0) | 2024.05.27 |
[MAC/Homebrew] JAVA 11 설치 (0) | 2023.10.25 |
[Mac/Intellij] 포트 겹쳤을 때 (0) | 2023.10.20 |
[Mac/Intellij] JSP 인코딩 설정 (UTF-8) (0) | 2023.10.13 |