코딩항해기

[Error/Spring] NoSuchBeanDefinitionException (xml 어노테이션 설정 문제) 본문

Error solution

[Error/Spring] NoSuchBeanDefinitionException (xml 어노테이션 설정 문제)

miniBcake 2024. 10. 4. 11:43

 

 

 

기존 xml bean 태그로 작업하다 어노테이션으로 변경했더니 bean을 찾지 못하는 에러가 발생했다.

xml에서 어노테이션을 인지할 수 있도록 총 2가지의 작업을 해야하는데, 범위지정이 빠져 발생한 오류였다.

 

기존 루트 앨리먼트

<?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">

 

 

어노테이션을 인지할 수 있도록 변경한 루트 앨리먼트

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

 

 

어노테이션을 찾을 범위를 지정해주는 태그

*test패키지 안에 있는 파일에서 어노테이션을 찾겠다는 설정이다. (해당 태그가 빠져 오류 발생)

   <context:component-scan base-package="test" />