0
此問題已在較早的線程中提出,但我的代碼仍然無法正常工作。 please click for the earlier threadXML配置中的Java Spring Framework JavaConfig
這是在Spring框架中混合佈線的情況。 我有一個彈簧佈線,我試圖從xml調用javaconfig beans,然後通過應用程序上下文調用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();
}
}
下面是我運行它時得到的錯誤。
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呼叫,但「詳細信息」和「形狀」沒有得到創建的Bean。 請幫助,讓我知道是否需要其他類的代碼。
謝謝!這工作。 – Learner
請標記接受的問題。 – mhshimul