spring xml配置中引用java配置不能用ClassPathXmlApplicationContext()

  本篇文章为你整理了spring xml配置中引用java配置不能用ClassPathXmlApplicationContext()的详细内容,包含有 spring xml配置中引用java配置不能用ClassPathXmlApplicationContext,希望能帮助你了解 spring xml配置中引用java配置不能用ClassPathXmlApplicationContext。

   private String title = "Sgt. Peppers Lonely Hearts Club Band";

   private String artist = "The Beatles";

   public void play() {

   System.out.println("Playing " + title + " by " + artist);

  }

 

 

  播放器接口

  

package v4.c2;

 

  public interface MediaPlayer {

   void play();

  }

 

  播放器实现类:

  

package v4.c2;

 

  import org.springframework.beans.factory.annotation.Autowired;

  public class CDPlayer implements MediaPlayer {

   private CompactDisc cd;

   @Autowired

   public CDPlayer(CompactDisc cd) {

   this.cd = cd;

   public void play() {

   cd.play();

  }

 

  CD配置类:

  

package v4.c2;

 

  import org.springframework.context.annotation.Bean;

  import org.springframework.context.annotation.Configuration;

  @Configuration

  public class CDConfig {

   @Bean

   public CompactDisc compactDisc() {

   System.out.println(2222);

   return new SgtPeppers();

  }

 

  播放器的配置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 /

   bean id="cdPlayer"

   c:cd-ref="compactDisc" /

   /beans

 

  注意这里就是在xml配置中引用java配置

  

 bean / 

 

  现在用ClassPathXmlApplicationContext测试

  

package v4.c2;

 

  import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class Boot {

   public static void main(String[] args) {

   ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("v4/c2/cdplayer-config.xml");

   MediaPlayer media = ac.getBean(MediaPlayer.class);

   media.play();

   ac.close();

  }

 

  报错:

  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named compactDisc available

  换成AnnotationConfigApplicationContext的方式,要添加一个根配置类:

  

package v4.c2;

 

  import org.springframework.context.annotation.Configuration;

  import org.springframework.context.annotation.Import;

  import org.springframework.context.annotation.ImportResource;

  @Configuration

  @ImportResource("classpath:v4/c2/cdplayer-config.xml")

  public class SoundSystemConfig {

  }

 

  这样测试:

  

package v4.c2;

 

  import org.springframework.context.annotation.AnnotationConfigApplicationContext;

  public class Boot {

   public static void main(String[] args) {

   AnnotationConfigApplicationContext ac =new AnnotationConfigApplicationContext(SoundSystemConfig.class);

   MediaPlayer media = ac.getBean(MediaPlayer.class);

   media.play();

   ac.close();

  }

 

  成功,输出:

  2222
Playing Sgt. Peppers Lonely Hearts Club Band by The Beatles

  以上就是spring xml配置中引用java配置不能用ClassPathXmlApplicationContext()的详细内容,想要了解更多 spring xml配置中引用java配置不能用ClassPathXmlApplicationContext的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: