2017-02-22 1 views
0

Diese Frage wurde bereits früher unter Thread gestellt, aber mein Code funktioniert immer noch nicht. please click for the earlier threadJava Spring Framework JavaConfig in XML-Konfiguration

Dies ist der Fall der gemischten Verdrahtung in Spring Framework. Ich habe eine Feder Verdrahtung, wo ich versuche, JavaConfig-Beans von XML und dann aufrufen Xml über Anwendungskontext, aber immer noch Fehler.

-Code Details sind unten:

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 Klasse

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-basierte Anwendungskontext Aufruf

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(); 
     } 
} 

Unten ist der Fehler, den ich bekomme, wenn ich es laufe.

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) 

Sieht aus wie JavaConfig Anruf gemacht wird, sondern ‚Details‘ und ‚Shape‘ Bohnen werden nicht erstellt zu werden. Bitte helfen Sie und lassen Sie mich wissen, wenn Codes anderer Klassen erforderlich sind.

Antwort

1

Versuchen Sie, <context:annotation-config/> in Ihrer Beanconfig.xml hinzuzufügen. Denn während <context:annotation-config/> eingeschaltet ist, erkennt der Container die Annotation @Configuration und verarbeitet die in AppConfig deklarierten @Bean-Methoden ordnungsgemäß.

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

Danke! Das hat funktioniert. – Learner

+0

Bitte markieren Sie die akzeptierte Frage. – mhshimul

Verwandte Themen