Java Spring Framework JavaConfig in XML 配置

Java Spring Framework JavaConfig in XML configuration

这个问题之前已经在下面的线程中提出过,但我的代码仍然无法正常工作。 please click for the earlier thread

这是Spring框架中混合布线的情况。 我有一个 spring 连接,我试图从 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 class

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)

Looks like JavaConfig call is being made but 'Details' and 'Shape' beans are not getting created. Please help and let me know if codes of other classes are required.

尝试在 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>