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);
- }
- }
********配置文件******
- <?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
- http://www.springframework.org/schema/context">
- <bean class="beanConfigByXml.SgtPeppers"
- c:_-ref="sgtPeppers" />
- <bean class="beanConfigByXml.CDPlayer"
- c:_-ref="cdPlayer" />
- <!-- -->
***********测试代码*************
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