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.
Danke! Das hat funktioniert. – Learner
Bitte markieren Sie die akzeptierte Frage. – mhshimul