2017-02-22 3 views
0

이 질문은 아래 스레드에서 이전에 질문했지만 내 코드는 여전히 작동하지 않습니다. please click for the earlier threadJava Spring Framework XML 구성의 JavaConfig

이것은 Spring Framework의 혼합 배선의 경우입니다. 스프링 배선을 사용하여 xml에서 javaconfig 빈을 호출하고 응용 프로그램 컨텍스트를 통해 xml을 호출하지만 여전히 오류가 발생합니다.

코드의 세부 사항은 다음과 같습니다 :

beanconfig.xml

<?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:c="http://www.springframework.org/schema/c" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

     <bean class="org.spring.wiring.javabasd.appcontextxml.JavaConfig"> 
     </bean>  
</beans> 

JavaConfig 클래스

package org.spring.wiring.javabasd.appcontextxml; 
import org.springframework.context.annotation.Bean; 
    @Configuration 
    class JavaConfig { 
     @Bean 
     public Shape xyz(){ 
      return new Sphere();   
     } 

     @Bean 
     public Details abc(){  
      return new Details(xyz()); 
     } 
    } 

XML 기반 애플리케이션 컨텍스트 전화

package org.spring.wiring.javabasd.appcontextxml; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

class Main { 
    public static void main(String[] args) throws Exception { 

     ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:org/spring/wiring/javabasd/appcontextxml/beanconfig.xml"); 
     Details gg = context.getBean(Details.class); 

     gg.getVolume(); 
     context.close(); 
     } 
} 

다음은 실행했을 때의 오류입니다. JavaConfig의 호출이 있지만, '세부 사항'과 콩이 만들어지고 있지 '모양'되고처럼

Feb 21, 2017 9:08:25 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]b1a58a3: startup date [Tue Feb 21 21:08:25 EST 2017]; root of context hierarchy 
Feb 21, 2017 9:08:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [org/spring/wiring/javabasd/appcontextxml/beanconfig.xml] 
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.spring.wiring.javabasd.appcontextxml.Details] is defined 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:374) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088) 
    at org.spring.wiring.javabasd.appcontextxml.Main.main(Main.java:9) 

보인다. 다른 클래스의 코드가 필요한 경우 알려주십시오.

답변

1

beanconfig.xml에 <context:annotation-config/>을 추가하십시오. <context:annotation-config/>이 켜져 있기 때문에 컨테이너는 @Configuration 주석을 인식하고 AppConfig에 선언 된 @Bean 메소드를 올바르게 처리합니다.

<?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:c="http://www.springframework.org/schema/c" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

     <context:annotation-config/> 

     <bean class="org.spring.wiring.javabasd.appcontextxml.JavaConfig"> 
     </bean>  
</beans> 
+0

고마워요! 이것은 효과가 있었다. – Learner

+0

동의하는 질문에 표시를하십시오. – mhshimul