Reputation: 23
This question has been asked earlier at below thread but my code is still not working. please click for the earlier thread
This is the case of mixed wiring in Spring Framework. I have a spring wiring where I am trying to invoke javaconfig beans from xml and then invoking xml via application context but still getting error.
Code details are below:
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());
}
}
XMLbased Application Context Call
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();
}
}
Below is the error I get when I run it.
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.
Upvotes: 0
Views: 2167
Reputation: 8386
Try adding <context:annotation-config/>
in your beanconfig.xml. Because while <context:annotation-config/>
is switched on, the container will recognize the @Configuration annotation and process the @Bean methods declared in AppConfig properly.
<?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>
Upvotes: 2