Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. @Component
  2. public class CommandFactory implements ApplicationContextAware {
  3.  
  4. private Map<String, Command> commandMap = new HashMap <>();
  5. private ApplicationContext context;
  6.  
  7. @Override
  8. public void setApplicationContext(ApplicationContext applicationContext) throw BeansException{
  9. context = applicationContext;
  10. }
  11.  
  12. @PostConstruct
  13. public void registerCommand() {
  14. ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
  15. provider.addIncludeFilter(new AssignableTypeFilter(Command.class));
  16. Set<BeanDefinition> components = provider.findCandidateComponents(Command.class.getPackage().getName());
  17.  
  18. for (BeanDefinition component : components) {
  19. try {
  20. Object bean = context.getBean(Class.forName(component.getBeanClassName()));
  21. if (bean instanceof Command) {
  22. Command command = (Command) bean;
  23. commandMap.put(command.getName(), command);
  24. }
  25. } catch (ClassNotFoundException e) {
  26. log.error(e.getMessage());
  27. }
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment