zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1.

    package soundsystem;
    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();
      }
    
    }

    2.

     1 public class SgtPeppers implements CompactDisc {
     2 
     3   private String title = "Sgt. Pepper's Lonely Hearts Club Band";  
     4   private String artist = "The Beatles";
     5   
     6   public void play() {
     7     System.out.println("Playing " + title + " by " + artist);
     8   }
     9 
    10 }

    3.

     1 package soundsystem;
     2 
     3 import java.util.List;
     4 
     5 public class BlankDisc implements CompactDisc {
     6 
     7   private String title;
     8   private String artist;
     9   private List<String> tracks;
    10 
    11   public BlankDisc(String title, String artist, List<String> tracks) {
    12     this.title = title;
    13     this.artist = artist;
    14     this.tracks = tracks;
    15   }
    16 
    17   public void play() {
    18     System.out.println("Playing " + title + " by " + artist);
    19     for (String track : tracks) {
    20       System.out.println("-Track: " + track);
    21     }
    22   }
    23 
    24 }

    一、在Java配置文件中引入Java配置文件

    1.

     1 package soundsystem;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 
     6 @Configuration
     7 public class CDConfig {
     8   @Bean
     9   public CompactDisc compactDisc() {
    10     return new SgtPeppers();
    11   }
    12 }

    2.则在CDPlayerConfig中可直接用@import引入上面的配置或新建一个文件引入这两个配置文件

    (1)

    1 @Configuration
    2 @Import(CDConfig.class)
    3 public class CDPlayerConfig {
    4 @Bean
    5 public CDPlayer cdPlayer(CompactDisc compactDisc) {
    6 return new CDPlayer(compactDisc);
    7 }
    8 }

    (2)

    1 package soundsystem;
    2 import org.springframework.context.annotation.Configuration;
    3 import org.springframework.context.annotation.Import;
    4 @Configuration
    5 @Import({CDPlayerConfig.class, CDConfig.class})
    6 public class SoundSystemConfig {
    7 }

    二、在Java配置文件中引入xml配置文件,可用@ImportResource

    1.cd-config.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4   xmlns:c="http://www.springframework.org/schema/c"
     5   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7   <bean id="compactDisc"
     8         class="soundsystem.BlankDisc"
     9         c:_0="Sgt. Pepper's Lonely Hearts Club Band"
    10         c:_1="The Beatles">
    11     <constructor-arg>
    12       <list>
    13         <value>Sgt. Pepper's Lonely Hearts Club Band</value>
    14         <value>With a Little Help from My Friends</value>
    15         <value>Lucy in the Sky with Diamonds</value>
    16         <value>Getting Better</value>
    17         <value>Fixing a Hole</value>
    18         <!-- ...other tracks omitted for brevity... -->
    19       </list>
    20     </constructor-arg>
    21   </bean>
    22 
    23 </beans>

    2.SoundSystemConfig.java

     1 package soundsystem;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.context.annotation.Import;
     5 import org.springframework.context.annotation.ImportResource;
     6 
     7 @Configuration
     8 @Import(CDPlayerConfig.class)
     9 @ImportResource("classpath:cd-config.xml")
    10 public class SoundSystemConfig {
    11 
    12 }

    Both beans— CDPlayer configured in JavaConfig and BlankDisc configured in XML —
    will be loaded into the Spring container. And because CDPlayer ’s @Bean method
    accepts a CompactDisc as a parameter, the BlankDisc bean will be wired into it, even
    though it’s configured in XML .

     

  • 相关阅读:
    【html】页面制作规范文档
    【jquery】blockUI 弹出层
    前端攻城师所要掌握的知识和技能
    【html】edm 邮件制作指南
    【css】教你如何写出高效整洁的 css 代码——css优化
    前端开发神器notepad++以及zen coding神级插件
    百度统计流量研究院——了解互联网行业基本数据分布和趋势
    【css】我的 css 框架——base.css
    通过扩展方法 链式方法 为MVC 3 视图添加验证
    使用正则表达式抓取博客园列表数据
  • 原文地址:https://www.cnblogs.com/shamgod/p/5234865.html
Copyright © 2011-2022 走看看