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 .
You can do anything you set your mind to, man!