zoukankan      html  css  js  c++  java
  • 通过处理器类型获得处理器对象

    使用两种方法通过处理器类型获得处理器对象

    1、通过@postConstruct注解,构造函数之后注册对象

    2、通过继承ApplicationContextAware接口,实现setApplicationContext方法。

    package rpg.test;
    import javax.annotation.PostConstruct;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public abstract class AbstractHandler {
    
        @Autowired
        private BManager bmanager;
    
        @PostConstruct
        public void init() {
            bmanager.regist(this);
            System.out.println("加载---"+this);
        }
    
        public abstract TestType getType();
    
    }
    View Code
    package rpg.test;
    import org.springframework.stereotype.Component;
    
    @Component
    public class AHandler extends AbstractHandler {
    
        @Override
        public TestType getType() {
            return TestType.A;
        }
    
    }
    View Code
    package rpg.test;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BHandler extends AbstractHandler {
    
        @Override
        public TestType getType() {
            return TestType.B;
        }
    
    }
    View Code
    package rpg.test;
    
    public enum TestType {
        A,
        B,
        C,
        D,
        E;
    }
    View Code
    package rpg.test;
    import java.util.Collection;
    import java.util.EnumMap;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component
    public class AManager implements ApplicationContextAware {
    
        private EnumMap<TestType, AbstractHandler> handlers;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            Collection<AbstractHandler> collection = applicationContext.getBeansOfType(AbstractHandler.class).values();
            handlers = new EnumMap<>(TestType.class);
            for (AbstractHandler abstractHandler : collection) {
                handlers.put(abstractHandler.getType(), abstractHandler);
                System.out.println(this+"加载---"+abstractHandler);
            }
        }
    
        public AbstractHandler getHandler(TestType testType) {
            return handlers.get(testType);
        }
    }
    View Code
    package rpg.test;
    import java.util.EnumMap;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BManager {
        private EnumMap<TestType, AbstractHandler> handlers = new EnumMap<>(TestType.class);
    
        public void regist(AbstractHandler handler) {
            handlers.put(handler.getType(), handler);
        }
    
        public AbstractHandler getHandler(TestType testType) {
            return handlers.get(testType);
        }
    }
    View Code
    package rpg.test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestMain {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:server.xml");
            AManager aManager = (AManager) context.getBean("AManager");
            AbstractHandler handler = aManager.getHandler(TestType.B);
            System.out.println(handler);
        }
    
    }
    View Code
  • 相关阅读:
    java发送邮件
    MySQL查询表结构的SQL语句
    Jquery的toggle()方法
    jQuery为图片添加链接(创建新的元素来包裹选中的元素)
    mysql修改存储过程的权限
    php中接收参数,不论是来自GET还是POST方法
    解决php中文乱码
    MySQL的视图view,视图创建后,会随着表的改变而自动改变数据
    选项卡面向对象练习
    对数组的操作splice() 和slice() 用法和区别
  • 原文地址:https://www.cnblogs.com/CrazyBaby/p/11453794.html
Copyright © 2011-2022 走看看