All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mybatis.spring.ClassScanner Maven / Gradle / Ivy

The newest version!
/**
 *    Copyright 2010-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.spring;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wupeng  * @version 1.0  * @date 2015-10-30  * @modify  * @copyright Navi Tsp
 */
public class ClassScanner {

    public static List scan(String scanPackage) {
        List clazzs = new LinkedList();
        String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(scanPackage) + "/**/*.class";
        try {             /**              *              * 扫描包 然后加载class              *              */
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    Class clazz = Class.forName(className);
                    clazzs.add(clazz);
                }
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Error scanning package: ".concat(scanPackage), e);
        } catch (IOException e) {
            throw new IllegalStateException("Error scanning package: ".concat(scanPackage), e);
        }

        return clazzs;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy