본문 바로가기
프로그래밍

[Spring Boot] bean 에러 해결 방법 AppConfig 클래스 생성

by 개발자신입 2024. 6. 24.
반응형

에러 내용

문제는 WeatherService 클래스에서 RestTemplate 빈을 찾을 수 없다는 것입니다. 스프링 부트 애플리케이션에서 RestTemplate를 사용하려면 빈으로 등록해야 합니다. 

Description:

Field restTemplate in com.pj.web.service.WeatherService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

 

해결 방법

AppConfig 클래스를 생성해서 bean으로 등록해준다. 그 후 서비스에서 autowired로 불러와서 사용 가능.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 

 

반응형

댓글