• 11_注解03_用注解方式决定接口的实现类


    【工程截图】

    【Document.java】

    package com.HigginCui.document.annotation;
    
    public interface Document {
        public void readDocument();
        public void writeDocument();
    }

    【ExcelDocument.java】

    package com.HigginCui.document.annotation;
    
    import org.springframework.stereotype.Component;
    
    @Component("excelDocument")
    public class ExcelDocument implements Document{
        @Override
        public void readDocument() {
            System.out.println("Excel read....");
        }
        @Override
        public void writeDocument() {
            System.out.println("Excel write....");
        }
    }

    【PdfDocument.java】

    package com.HigginCui.document.annotation;
    
    import org.springframework.stereotype.Component;
    
    @Component("pdfDocument")
    public class PdfDocument implements Document{
        @Override
        public void readDocument() {
            System.out.println("Pdf read....");
        }
        @Override
        public void writeDocument() {
            System.out.println("Pdf write....");
        }
    }

    【WordDocument.java】

    package com.HigginCui.document.annotation;
    
    import org.springframework.stereotype.Component;
    
    @Component("wordDocument")
    public class WordDocument implements Document{
        @Override
        public void readDocument() {
            System.out.println("Word read....");
        }
        @Override
        public void writeDocument() {
            System.out.println("Word write....");
        }
    }

    【DocumentManager.java】

    package com.HigginCui.document.annotation;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Component;
    
    @Component("documentManager")
    public class DocumentManager {
    
        @Resource(name="excelDocument")
        private Document document;
        
        public void read(){
            this.document.readDocument();
        }
        public void write(){
            this.document.writeDocument();
        }
    }

    【applicationContext.xml】

    <?xml version= "1.0" encoding ="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- 把一个类放入到Spring容器中,该类就是一个component,此时不需要声明对应的bean -->
        <context:component-scan base-package="com.HigginCui.document.annotation"></context:component-scan>
    </beans>

    【testDocument.java】

    package com.HigginCui.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.HigginCui.document.annotation.DocumentManager;
    
    
    public class testDocument {
        @Test
        public void test(){
            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            DocumentManager documentManager=(DocumentManager) context.getBean("documentManager");
            documentManager.write();
            documentManager.read();
        }
    }

    【运行结果】

    Excel write....
    Excel read....
  • 相关阅读:
    Java读书笔记
    b_aw_旅游计划(树的中心变形)
    b_lc_秋叶收集器(分类讨论dp+滚动数组优化)
    b_lg_涂色(从小区间做起,讨论s[l]和s[r]的关系)
    c_lc_早餐组合(排序+双指针)
    c_aw_鱼塘钓鱼(大根堆)
    b_pat_栈(2*multiset+stack)
    c_pat_推荐系统(set模拟)
    b_lg_时态同步(后序遍历统计每棵子树的最大高度)
    b_lc_统计不开心的朋友(预处理+模拟)
  • 原文地址:https://www.cnblogs.com/HigginCui/p/5574580.html
Copyright © 2020-2023  润新知