zoukankan      html  css  js  c++  java
  • Spring------自动化装配Bean(一)

    一、创建 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播放。

      

    你都没坚持,还谈什么未来
  • 相关阅读:
    【BZOJ 2565】 最长双回文串
    【BZOJ 2160】 拉拉队排练
    【POI 2010】 Antisymmetry
    【HDU 3068】 最长回文
    【POJ 3974】 Palindrome
    【POJ 2503】 Babelfish
    【POJ 3349】 Snowflake Snow Snowflakes
    【BZOJ 2457】 双端队列
    根文件系统的构建与分析(一)之流程分析
    Linux MTD系统剖析
  • 原文地址:https://www.cnblogs.com/gede/p/10889762.html
Copyright © 2011-2022 走看看