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
  • 相关阅读:
    Cocos2d-x 3.2编译生成Android程序出错Error running command, return code: 2的解决方法
    利用Theme自定义Activity进入退出动画
    Activity的四种launchMode
    android中设置控件获得焦点
    android 反编译 for mac
    android中libs目录下armeabi和armeabi-v7a的区别
    解决Sublime Text 3中文显示乱码(tab中文方块)问题,sublime tab乱码
    mysql教程
    Failed to load c++ bson extension, using pure JS version
    mongodb导出数据
  • 原文地址:https://www.cnblogs.com/CrazyBaby/p/11453794.html
Copyright © 2011-2022 走看看