zoukankan      html  css  js  c++  java
  • 【第一节】装配Bean的方案

    一、相关概念

      装配(wiring):创建应用对象之间协作关系的行为;

    二、Spring配置的可选方案

      Spring容器负责创建应用程序中bean,并通过DI来协调这些对象之间的关系。Spring是如何装配bean了?其中主要有三种方式,如下:

    1 在XML中进行显示配置;
    2 在java中使用javaconfig进行显示配置;
    3 隐式的bean发现机制和自动装配;

    其中,最为推荐的方式是自动装配,但有时候不得已的情况下,不得不使用显示的装配,接下来状态演示一下三种装配。

    2.1  隐式bean的自动装配

      在隐式bean的自动装配中,spring会从两个角度实现自动装配:  

      1、组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
      2、自动装配(autowiring):Spring 自动满足bean之间的依赖。
    组件扫描自动装配组合在一起就能发挥出强大的威力,它们能将显示配置降低到最小。
    在自动装配中,常常会使用下列组件:
    1、@Component:这个注释表明该类会作为组件类,并告知spring要为这个类创建bean。
    2、@Configuration:
    3、@ComponentScan():这个注解表示自动扫描包中的组件,其中括号中可以填写basepackageClasses={}或basepackages={}两个,baskpackageClass的数组中包含了一些类,这类所在的包将会作为组件扫描的基础包;basepackages的数组中包含了要作为扫描组件的基础包;
    4、@contextConfiguration():这个注解表示会告诉它需要将哪个文件作为配置文件加载;
    5、@AutoWired:这个注解表示自动加载;
    

     演示代码,功能主要是CD播放器播放CD

    CD的接口类:
    package soundsystem;
    
    public interface CompactDisc {
    	void play();
    }
    

      定义其中一个正式的CD类:

    package soundsystem;
    
    import org.springframework.stereotype.Component;
    
    /*
     * @Component注解表示这个是一个组件,可以被作为bean加载的容器中去;
     */
    @Component
    public class sgtPeppers implements CompactDisc{
    	
    	private String title = "Sgt.Pepper's Lonely Hearts Club Band";
    	private String artist =  "The Beatles";
    	
    	public void play(){
    		System.out.println("Playing " + title +" by " + artist);
    	}
    }
    

    定义一个CD播放器:

    package soundsystem;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class CDPlayer implements MediaPlayer{
    	
    	private CompactDisc cd;
    	
    	/*
    	 * @Autowired 使用构造器自动装载,其中CompactDisc就是一个cd,具体在加载中确定
    	 * 这个感觉有点像java中多态
    	 */
    	@Autowired
    	public CDPlayer(CompactDisc cd){
    		this.cd =  cd;
    	}
    	
    	public void play(){
    		cd.play();
    	}
    }
    

    javaconfig文件:

    package soundsystem;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.stereotype.Component;
    
    /*
     * @Component:表示组件,不再说了
     * @ComponentScan:表示可以扫描类中所有组件,并加入到spring容器中去,basePackages这里是可选项,如果不写表示本这个类所在的包
     */
    @Component
    @ComponentScan(basePackages="soundsystem")
    public class CDPlayConfig {
    
    }
    

     对上面装配进行测试:

    package soundsystem;
    
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.contrib.java.lang.system.SystemOutRule;
    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;
    
    import junit.framework.Assert;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes=CDPlayConfig.class)
    public class CDPlayTest {
    	
    	@Rule
    	public final SystemOutRule log = new SystemOutRule(); 
    	
    	/*
    	 * 自动装配CompactDisc组件
    	 */
    	@Autowired
    	private CompactDisc cd;
    	
    	/*
    	 * 自动装配CDPlayer组件
    	 */
    	@Autowired
    	private CDPlayer player;
    	
    	@Test
    	public void cdShouldNotBeNull(){
    		Assert.assertNotNull(cd);
    	}
    	
    	@Test
    	public void play(){
    		player.play();
    		String str = log.getLog();
    		Assert.assertEquals(str, "Playing Sgt.Pepper's Lonely Hearts Club Band by The Beatles");
    	}
    }
    

      

    2.2 使用javaconfig装配

    待插入

    2.3使用xml对bean进行装配
    使用xml进行装配的配置方案有两种:
      1、原始的<constructor-arg>元素;
      2、使用Spring3.0中引入的c-命令空间和p-命令空间进行装配;
    这两中装配各有自己的优缺点,需自己去了解一下就可以了,这里主要说一下使用第一种方式进行装配;

    还是使用上面的例子进行解释:

    CD类:
    package soundsystem;
    
    
    public class sgtPeppers implements CompactDisc{
    	
    	private String title = "Sgt.Pepper's Lonely Hearts Club Band";
    	private String artist =  "The Beatles";
    	
    	public void play(){
    		System.out.println("Playing " + title +" by " + artist);
    	}
    }
    

     播放器类:

    package soundsystem;
    
    public class CDPlayer implements MediaPlayer{
    	
    	private CompactDisc cd;
    	
    	public CDPlayer(CompactDisc cd){
    		this.cd =  cd;
    	}
    	
    	public void play(){
    		cd.play();
    	}
    }
    

     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:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
    			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    
    	<!-- 将cd类作为bean加载到spring容器中 -->
    	<bean id="compactDisc" class="soundsystem.sgtPeppers">
    	</bean>
    	
    	<!-- 将播放器作为bean加载到spring容器中 -->
    	<bean id="cdplayer" class="soundsystem.CDPlayer">
    		<!-- 1、使用构造器元素添加 <constructor-arg ref="compactDisc"></constructor-arg> -->
    		 <constructor-arg ref="compactDisc"></constructor-arg>
    		<!-- 2、使用c命令空间的方式  :在bean里添加 c:cd-ref="compactDisc" -->
    
    	</bean>
    	
    </beans>
    			
    

     测试方式:

    package soundsystem;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		/*
    		 * 
    		 */
    		ApplicationContext apc = new ClassPathXmlApplicationContext("applicationContext.xml");
    		//从容器读取相对应的bean对象
    		CDPlayer cdp = (CDPlayer) apc.getBean("cdplayer");
    		
    		cdp.play();
    	}
    
    }
    

      





  • 相关阅读:
    服务器端接受Json数据的绑定实现
    SQL 学习笔记
    asp.net mvc下的多语言方案 包含Html,Javascript和图片
    设计和创建自己的Sharepoint Site
    SharePoint类库简要总结
    TED-谷歌创始人演示谷歌眼睛
    为什么要有战争
    跨云应用部署:共享数据存储
    使用VNET-to-VNET连接Microsoft Azure国际版和中国版
    MySQL Database on Azure新功能
  • 原文地址:https://www.cnblogs.com/rolly-yan/p/5679347.html
Copyright © 2011-2022 走看看