Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class CommandFactory implements ApplicationContextAware {
- private Map<String, Command> commandMap = new HashMap <>();
- private ApplicationContext context;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throw BeansException{
- context = applicationContext;
- }
- @PostConstruct
- public void registerCommand() {
- ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
- provider.addIncludeFilter(new AssignableTypeFilter(Command.class));
- Set<BeanDefinition> components = provider.findCandidateComponents(Command.class.getPackage().getName());
- for (BeanDefinition component : components) {
- try {
- Object bean = context.getBean(Class.forName(component.getBeanClassName()));
- if (bean instanceof Command) {
- Command command = (Command) bean;
- commandMap.put(command.getName(), command);
- }
- } catch (ClassNotFoundException e) {
- log.error(e.getMessage());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment