Spring注入,在XML配置中引用JavaConfig,结果找不到Config中的bean
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<bean id="sgtPeppers" class="com.soundsystem.SgtPeppers"/>-->
<!--引入JavaConfing配置类-->
<bean class="com.soundsystem.CDConfig"/>
<!--xml配置-->
<bean id="cdPlayer" class="com.soundsystem.CDPlayer">
<constructor-arg ref="sgtPeppers"/>
</bean>
</beans>
sgtPeppers的bean配置在JavaConfig中:
package com.soundsystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CDConfig {
@Bean
public CompactDisc sgtPeppers(){
return new SgtPeppers();
}
}
执行后报错:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cdPlayer' defined in class path resource [META-INF/spring/cdplayer-config.xml]: Cannot resolve reference to bean 'sgtPeppers' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sgtPeppers' available
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cdPlayer' defined in class path resource [META-INF/spring/cdplayer-config.xml]: Cannot resolve reference to bean 'sgtPeppers' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sgtPeppers' available
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sgtPeppers' available
IDEA显示是绿色的,但是为什么找不到
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开启注解
<context:annotation-config />