• 通过处理器类型获得处理器对象


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

    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
  • 相关阅读:
    【OpenCv/EmguCv】指针式仪表读数(二)
    合天网安实验室CTF练习赛之RE300
    Codeforces Round #527 -A. Uniform String(思维)
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/CrazyBaby/p/11453794.html
Copyright © 2020-2023  润新知