用xml配置bean时显示以下错误是什么原因?

The prefix "c" for attribute "c:_-ref" associated with an element type "bean

************定义一个接口********************

package beanConfigByXml;

  • public interface CompactDisc {

  • void play();
  • }

************实现类1********************

package beanConfigByXml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class CDPlayer implements CompactDisc{

  • @Autowired
  • private CompactDisc cd;
  • @Override
  • public void play() {
  • cd.play();

  • }

}

************实现类2********************

package beanConfigByXml;
import org.springframework.stereotype.Component;

  • public class SgtPeppers implements CompactDisc{
  • private String title="Sgt.Pepper's Lonely Hearts Club Band";
  • private String artist="The Beatles";
  • @Override
  • public void play() {
  • // TODO Auto-generated method stub
  • System.out.println("Playing"+title+"by"+artist);
  • }
  • }

********配置文件******

***********测试代码*************

package beanConfigByXml;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

  • @RunWith(SpringJUnit4ClassRunner.class)
  • public class CDPlayerTest {
  • @Autowired
  • private CompactDisc cd;
  • @Test
  • public void cdShouldNotBeNull() {
  • cd.play();
  • assertNotNull(cd);
  • }

xml配置就报错误,刚接触Spring,大神们看看~
The prefix "c" for attribute "c:_-ref" associated with an element type "bean

bean标签没有c:_-ref这个属性吧

spring bean是没有这个属性的,参考地址:http://blog.csdn.net/supportuat/article/details/50652935。这个属性从命名角度就是错误的,完全乱写的

****************配置文件作如下修改,增加c-空间命名*********************

  • <?xml version="1.0" encoding="UTF-8"?>

  • <beans xmlns="http://www.springframework.org/schema/beans"
    * xmlns:c="http://www.springframework.org/schema/c"

  •   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
    
  •   http://www.springframework.org/schema/context">
    
  • <!-- -->

  • <bean id="myplayer" class="beanConfigByXml.CDPlayer"

  •   c:_-ref="mysgtPeppers" />
    
  • <!-- -->

*****************测试类中作如下修改*****************
package beanConfigByXml;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import beanConfigForJava.CDPlayerConfigForJava;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="configXml.xml") //上下文配置文件java配置中使用classes=“CDPlayerConfigForJava.class”
public class CDPlayerTest {
@Autowired
private CDPlayer cd;
@Test
public void cdShouldNotBeNull() {
cd.play();
assertNotNull(cd);
}

}
CDPlayer中应用了SgtPeppers bean,这里注入CDPlayer bean就可以间接引用SgtPeppers bean.
最后输出结果:
Playing Sgt.Pepper's Lonely Hearts Club Band by The Beatles