zoukankan      html  css  js  c++  java
  • 隐式的bean发现与自动装配机制

    使用beans.xml文件进行bean的创建和注入通常是可行的,但在便利性上Spring提供了更简单的方法——自动装配

    接下来我们假设一个场景:我有若干播放器(MediaPlayer{CD播放器/MP3}),我也有很多媒体文件例如(CompactDisc{CD光盘/MP3文件})。
    现在,我们需要创建两个接口MediaPlayer/CompactDisc,然后创建他们的实现CDPlayer/CompactDisc_zhoujielun。注意:CompactDisc_zhoujielun是周杰伦的CD光盘。代码如下:
     1 package com.spring.cd;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Component;
     5 
     6 @Component//Spring自动创建bean
     7 public class CDPlayer implements MediaPlayer{
     8     private CompactDisc cd;
     9     
    10     @Autowired//表明Spring初始化后尽可能地去满足bean的依赖,在这里它注入了一个CompactDisc的对象
    11     public CDPlayer(CompactDisc cd){
    12         this.cd=cd;
    13     }
    14     @Override
    15     public void player() {
    16         System.out.println("wo yong CD!");
    17     }
    18 }

    当然,我们也可以在创建bean时对它命名,在CDPlayer类中可以体会到。代码如下:

    package com.spring.cd;
    
    import org.springframework.stereotype.Component;
    
    @Component("ZhouJieLun")
    public class CompactDisc_zhoujielun implements CompactDisc{
    	private String title="发如雪";
    	private String artist="周杰伦";
    	
    	@Override
    	public void play(){
    		System.out.println("播放:"+title+"来自艺术家:"+artist);
    	}
    }
    
    接下来,我们需要启用组件扫描,组件扫描过程是通过Java代码来定义Spring的装配规则。代码如下:
    package com.spring.cd;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    @ComponentScan("com.spring.cd") //参数代表当前需要扫描的路径,为空默认为当前包路径
    @Configuration("cd")//需要扫描的包名称
    //通过java代码定义spring的装配机制
    public class CDPlayerConfig {
    
    }
    

     值得注意的是,真正的实现过程与代码主体非常复杂,@Component,@ComponScan,@Autowired,@Comfiguration等注解的使用方法与参数是多样的。

    为了实现可视化,我们新建一个JUnit4测试。代码如下:
    package com.spring.cd;
    
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes=CDPlayerConfig.class)
    public  class CDPlayerTest{
    	@Autowired
    	private CompactDisc cd;
    	@Autowired
    	private MediaPlayer player;
    	@Test
    	public void test() {
    		player.player();
    		cd.play();
    		assertNotNull(cd);
    		assertNotNull(player);
    	}
    
    }
    
    上下文创建成功。

    热爱分享拒绝拿来主义,博客精神永存——cvrigo

        2016-11-09 00:19:44

    支持原创,支持转载,拒绝抄袭
  • 相关阅读:
    西门子SCL读写DB数据
    LeetCode8.字符串转换整数(atoi) JavaScript
    LeetCode8.字符串转换整数(atoi) JavaScript
    WebRequestSugar
    iosblock用法
    datasci
    UINavigationController学习笔记
    iOSTab bar
    自定义tab bar控件 学习资料
    Ios tab Bar 使用方法
  • 原文地址:https://www.cnblogs.com/cvirgo/p/6045368.html
Copyright © 2011-2022 走看看