Spring------自動化裝配Bean(一) 一、創建 CompactDisc介面和SgetPeppers實現類二、啟用spring組件掃描三、編寫測試類,並運行 四、補充說明
- 2019 年 10 月 5 日
- 筆記
一、創建 CompactDisc介面和SgetPeppers實現類
CompactDisc介面方法為播放。SgtPeppers實現CompactDisc介面。
1 package soundsystem; 2 3 public interface CompactDisc { 4 void play(); 5 6 } 1 package soundsystem; 2 3 import org.springframework.stereotype.Component; 4 //component為spring中bean掃描標識 5 @Component 6 public class SgtPeppers implements CompactDisc { 7 8 private String title = "歌德"; 9 private String artist = "gede"; 10 11 public void play() { 12 System.out.println("Playing " + title + " by " + artist); 13 } 14 15 }二、啟用spring組件掃描
1、通過java配置啟用
添加 @Configuration @ComponentScan 兩個註解即可。
【注】使用ComponentScan時,若配置文件和bean在同一個包,省略基礎包備註也可以。
1 package soundsystem; 2 import org.springframework.context.annotation.ComponentScan; 3 import org.springframework.context.annotation.Configuration; 4 5 @Configuration 6 @ComponentScan("soundsystem") 7 public class JavaConfig { 8 9 }2、通過xml配置啟用:<context:component-scan base-package="soundsystem"></context:component-scan> 如果沒有自動提示或者報錯,在namespace中添加context配置
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="soundsystem"></context:component-scan> </beans>
三、編寫測試類,並運行
1、創建test包,分別通過java配置和xnl配置實現測試
java配置為:AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class);
xml配置為:ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
1 package test; 2 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import soundsystem.CompactDisc; 6 import soundsystem.SgtPeppers; 7 8 public class Test { 9 public static void main(String[] args) { 10 //基於java類中配置上下文 11 //AnnotationConfigApplicationContext context = 12 new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class); 13 //基於xml配置上下文 14 ClassPathXmlApplicationContext context = 15 new ClassPathXmlApplicationContext("applicationContext.xml"); 16 17 CompactDisc cd=context.getBean(SgtPeppers.class); 18 cd.play(); 19 } 20 }四、補充說明
1、spring中設置bean的id有兩種方式,一是用戶給出,二是系統自己生成默認id(默認將類名首字母變成小寫作為bean的id)。
用戶常用指定id方法為:
@Component("id") //也可以使用named屬性 @Named(「id」)2、關於組件掃描的基礎包,上面提到部分,指定單個基礎包,當有多個基礎包需要掃描時使用basePackages屬性。
1 @ComponentScan(basePackages={"soundsystem","test"})3、添加@Autowired註解實現自動裝配。添加介面MediaPlayer和實現類CDPlayer。修改Test測試類
1 package soundsystem; 2 public interface MediaPlayer { 3 void play(); 4 5 }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(); } } 1 public class Test { 2 public static void main(String[] args) { 3 //基於java類中配置上下文 4 //AnnotationConfigApplicationContext context = 5 new AnnotationConfigApplicationContext(soundsystem.JavaConfig.class); 6 //基於xml配置上下文 7 ClassPathXmlApplicationContext context = 8 new ClassPathXmlApplicationContext("applicationContext.xml"); 9 CompactDisc cd=context.getBean(SgtPeppers.class); 10 cd.play(); 11 MediaPlayer player=context.getBean(CDPlayer.class); 12 player.play(); 13 14 } 15 }4、備註CDPlayer重寫構造方法,通過Autowired自動添加構造方法CD對象,調用CD播放。