当前位置:   article > 正文

spring自定义注解及其处理_esmetadata注解

esmetadata注解

第一步定义一个注解。

第二步处理被注解的类,在处理时,可以实现两个类ApplicationListener, ApplicationContextAware:

  1. @Configuration
  2. public class CreateIndex implements ApplicationListener, ApplicationContextAware {
  3. @Autowired
  4. ElasticsearchIndex elasticsearchIndex;
  5. private Logger logger = LoggerFactory.getLogger(this.getClass());
  6. private ApplicationContext applicationContext;
  7. /**
  8. * 扫描ESMetaData注解的类,并自动创建索引mapping
  9. * @param event
  10. */
  11. @Override
  12. public void onApplicationEvent(ApplicationEvent event) {
  13. Map<String, Object> beansWithAnnotationMap = this.applicationContext.getBeansWithAnnotation(ESMetaData.class);
  14. beansWithAnnotationMap.forEach((beanName,bean) ->
  15. {
  16. try {
  17. if(!elasticsearchIndex.exists(bean.getClass())){
  18. elasticsearchIndex.createIndex(bean.getClass());
  19. logger.info("启动创建索引成功");
  20. }
  21. } catch (Exception e) {
  22. logger.error("创建索引不成功",e);
  23. }
  24. }
  25. );
  26. }
  27. @Override
  28. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  29. this.applicationContext = applicationContext;
  30. }

/**
     * Find all beans whose {@code Class} has the supplied {@link Annotation} type,
     * returning a Map of bean names with corresponding bean instances.
     * <p>Note that this method considers objects created by FactoryBeans, which means
     * that FactoryBeans will get initialized in order to determine their object type.
     * @param annotationType the type of annotation to look for
     * @return a Map with the matching beans, containing the bean names as
     * keys and the corresponding bean instances as values
     * @throws BeansException if a bean could not be created
     * @since 3.0
     */

Map<String, Object> org.springframework.beans.factory.ListableBeanFactory.getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException可以获取到被注解的类,结果为一个map:returning a Map of bean names with corresponding bean instances.(以sping bean名字为key,spring bean为值)。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/240297
推荐阅读
相关标签
  

闽ICP备14008679号