2012-10-19 4 views
0

저는 스프링에 비교적 익숙하지 않고 주석이 달리고 autowiring이 더 새롭습니다. 내 콩을 연결하는 방법을 알아낼 수 없습니다. 이 아이디어는 application-config.xml을 변경하여 CacheService와 NoCacheService간에 전환하는 것이지만 예외를 지나칠 수는 없습니다.스프링 3 MVC BeanCreationException - 클래스가 인터페이스입니다.

예외

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Service' defined in ServletContext resource [/WEB-INF/application-config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [webapp.Services]: Specified class is an interface 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848) 
<snip> 

직업

package webapp; 
interface Service { 
    List<String> get(); 
} 

@Service("cache") 
CacheService implements Service { 
    //from cache then from IO bound source 
    @Autowired 
    public CacheService(int v1, int v2) 
    ... 
} 

@Service("nocache") 
NoCacheService implements Service { 
    //from IO bound source 
    .... 
} 

컨트롤러 :

@RequestMapping("/") 
@Controller 
public class ServiceController { 

    @Autowired 
    Service service; 
    ... 
} 

애플리케이션 구성 XML :

<mvc:annotation-driven /> 
<context:component-scan base-package="webapp"/> 

<beans:bean id="Service" class="webapp.Service"> 
    <beans:property name="cache" ref="cache" /> 
</beans:bean> 


<beans:bean id="cache" class="webapp.CacheService"> 
    <beans:constructor-arg index="0" value="50"/> <!-- v1 --> 
    <beans:constructor-arg index="1" value="100"/> <!-- v2 --> 
</beans:bean> 

<beans:bean id="nocache" class="webapp.NoCacheService"> 
</beans:bean> 

답변

0

주석 또는 xml을 사용하여 항목을 연결하도록 선택할 수 있습니다.이 경우에는 양방향으로 구성되고보고있는 문제점을 유발하는 빈이 있습니다. 시도해보고 몇 가지 변경 사항을 나열 해 보겠습니다.

.1. webapp.Service이 인터페이스이며 오류 메시지의 원인 인 경우 <beans:bean id="Service" class="webapp.Service"> <beans:property name="cache" ref="cache" /> </beans:bean>을 제거하십시오.

.2. 구성을 통해 CacheService를 인스턴스화하는 것이 더 좋으므로 여기에서 @Service 주석을 제거하고 다음 내용 만 유지하면됩니다.

<beans:bean id="cache" class="webapp.CacheService"> 
    <beans:constructor-arg index="0" value="50"/> <!-- v1 --> 
    <beans:constructor-arg index="1" value="100"/> <!-- v2 --> 
</beans:bean> 

.3. NoCacheService는 xml 파일에있을 필요가 없습니다. 이미 수행 한 방식으로 component-scan을 사용하여 인스턴스화 할 수 있습니다.

.4.

@RequestMapping("/") 
@Controller 
public class ServiceController { 

    @Autowired @Qualifier("cache") //or noCache 
    Service service; 
    ... 
} 
+0

나는 "캐시"와 "NOCACHE"사이를 전환 할 수 있습니다 방법 : 이제 두 인스턴스가 있기 때문에 당신이 이런 식으로 할 수있는 컨트롤러로 주입하는 어느 표시해야의 CacheService 또는 NoCacheService을 injct하기 컨트롤러를 수정하지 않고? application-config xml에서 그렇게 할 수 있습니까? – meja

+0

컴포넌트 스캔 라인을 제거하고 xml의 모든 작업을 수행 할 수 있도록하려면 @Controller가 컴포넌트 스캔에 의해 자동으로 생성됩니다. 그런 다음 컨트롤러를 일반 bean으로 작성할 수 있습니다. ' –

+0

작동했습니다. 감사! – meja