2017-02-22 6 views
0

にこの質問は、以前のスレッド以下に頼まれましたが、私のコードはまだ機能していません。 please click for the earlier threadは、JavaのSpring FrameworkのJavaConfig XML構成

これは、Spring Frameworkの混合配線の場合です。 私はxmlからjavaconfig Beanを呼び出し、アプリケーションコンテキスト経由で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()); 
     } 
    } 

XMLbasedアプリケーション・コンテキストのコール

0123以下は
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(); 
     } 
} 

は、私はそれを実行したときに私が取得エラーです。

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) 

JavaConfigコールが行われているが、「詳細」と豆を作成取得されていません「形状」のように見えます。 他のクラスのコードが必要な場合は、教えてください。

答えて

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

関連する問題