XML配置中的Java Spring Framework JavaConfig

时间:2017-02-22 02:24:25

标签: xml spring spring-bean

这个问题早先在下面的帖子中被问过,但我的代码仍然无效。 please click for the earlier thread

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

基于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.springframework.context.support.ClassPathXmlApplicationContext@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 个答案:

答案 0 :(得分: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>