content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.spring;
  
  import java.util.HashSet;
  import java.util.Set;
  
  import org.miaixz.bus.core.lang.Symbol;
  import org.miaixz.bus.starter.Nexus;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Conditional;
  import org.springframework.core.Ordered;
  import org.springframework.core.annotation.Order;
  import org.springframework.core.io.Resource;
  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
  import org.springframework.core.type.classreading.MetadataReader;
  
  /**
   * Spring上下文等信息持有者 扫描包配置项及其他属性等
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  @ComponentScan("org.miaixz.**")
  @Conditional(Nexus.class)
  @Order(Ordered.HIGHEST_PRECEDENCE)
  public class SpringHolder {
  
      public static boolean alive = false;
  
      /**
       * 获取某个包下所有的class对象
       *
       * @param packageName 包路径
       * @return the object
       */
      public static Set<Class<?>> scan(String packageName) {
          Set<Class<?>> handlerSet = new HashSet();
          try {
              String pattern = "classpath*:" + packageName.replace(Symbol.C_DOT, Symbol.C_SLASH) + "/**/*.class";
              PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
              Resource[] resources = resourcePatternResolver.getResources(pattern);
              CachingMetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
              for (Resource resource : resources) {
                  try {
                      MetadataReader reader = readerFactory.getMetadataReader(resource);
                      String className = reader.getClassMetadata().getClassName();
                      Class<?> clazz = Class.forName(className);
                      handlerSet.add(clazz);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return handlerSet;
      }
  
  }
  