2017-02-22 55 views
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。 请帮助,让我知道是否需要其他类的代码。

回答

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