"
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显示是绿色的,但是为什么找不到
"
"
<bean id="cdPlayer" class="com.soundsystem.CDPlayer"> <constructor-arg ref="sgtPeppers"/> </bean> 意思是创建cdPlayer这个Bean的时候給CDPlayer的构造函数一个sgtPeppers对象
######
开启注解
<context:annotation-config />
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。