java/Spring

SpringBoot Bean 주입 에러(@ComponentScan)

킨글 2023. 7. 26. 22:57

***************************
APPLICATION FAILED TO START
***************************
Description:
Failed ExcelService in ExcelService required a bean of type 'com.skeleton.excel.service.ExcelService' that could not be found.

Action:

Consider defining a bean of type 'com.skeleton.excel.service.ExcelService' in your configuration

 

springboot에서 bean 생성이 com.skeleton.board 부분만 되고, com.skeleton.excel 부분은 되지 않는 이슈가 있었다. 

spring에서는 application.xml에서 bean을 주입해주지만, springboot에서는 그렇지 않기 때문에 다른 방법을 찾아야 했다.

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="demoService" class="com.skeleton.board.boardService">
        <property name="repository" ref="boardRepository"/>
        <property name="count" value="21"/>
    </bean>
    <bean id="boardRepository" class="com.skeleton.board.boardRepository"/>
</beans>

 

그래서 찾아본 결과, @ComponentScan을 활용해서 bean 주입을 해줄 package를 설정해줄 수 있었고, 정상 작동하게 되었다.

package com.skeleton.board;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.skeleton"})
public class BoardApplication {

	public static void main(String[] args) {
		SpringApplication.run(BoardApplication.class, args);
	}

}