본문 바로가기
Framework/Spring

[Spring 에러] Injection of resource dependencies failed #NoSuchBeanDefinitionException

by 나비와꽃기린 2016. 10. 14.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1.

Error creating bean with name '***Controller': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '***Service' is defined

(1) 서비스 명이 틀렸을 경우

필자 같은 경우는 (1)의 경우로 이런 에러가 났었는데 그 이유인 즉슨,

spring 설정파일인 context-mapper.xml (파일이름은 다 다르다~) 에 아래와 같이 sqlSessionTemplate로 설정해 놓고

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mapperLocations" >
		<list>
			<value>classpath*:com/common/sql/*.xml</value>
		</list>
	</property>
</bean>
 
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg index="0" ref="sqlSession"/> 
</bean>  

실제 사용하는 dao 클래스에서는 sqlSession 으로 사용하고 있었다... =_=;

@Autowired
@Resource(name="sqlSession")
private SqlSessionTemplate  sqlsession;


(2) 서비스를 정의하지 않았을 경우

 아래와 같이 @Service 나 @Repository 등 어노테이션을 지정해주지 않은 경우 발생할 수 있다.

어노테이션으로 의존관계를 주입해줘야 한다.

@Service
public class commonService {

@Repository
public class commonDao {


2.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.common.*****Service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

@Autowired하려는 빈을 등록하지 않았을 떄 나는 에러같다.


따라서 설정파일에 아래와 같이 작성해주어 해결하였다.

<!-- component scan (Controller) -->
<context:component-scan base-package="com.common.*">
	<context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="regex"
		expression="(service|controller|dao)\..*" />
</context:component-scan>
@Autowired하려는 변수에 객체를 빈을 통해 주입하는 방식을 위의 설정을 통해 
조금은 설정이 편리해진다..