Java實(shí)現(xiàn)SpringIOC類(lèi)裝載部分

實(shí)現(xiàn)Spring的IOC機(jī)制,當(dāng)然這里面沒(méi)有對(duì)類(lèi)進(jìn)行注解標(biāo)示(類(lèi)似Spring的@Service,@Configuration,@Controller),而是對(duì)全部類(lèi)進(jìn)行裝載,可以自定義注解后通過(guò)class.getAnnotations()獲取并篩選裝載。

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.google.gson.Gson;

public class PackageLoader {

    /**類(lèi)實(shí)例映射**/
    private static final Map<Class<?>, Object> MAP = new HashMap<Class<?>, Object>();
    
    public static void main(String[] args) {
        try {
            String packageName = "com.google";
            load(packageName);
            Gson o = getBean(Gson.class);
            System.out.println(o.getClass().getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 從已經(jīng)裝載的類(lèi)中獲取指的的實(shí)例
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz){
        Object object = MAP.get(clazz);
        return (T) object;
    }
    
    /**
     * 裝載類(lèi)
     * @param packageName
     * @return
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IOException
     */
    public static void load(String packageName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException{
        List<String> classNames = classNames(packageName);
        for(String cn : classNames){
            Class<?> clazz = Class.forName(cn);
            if(Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())){//抽象類(lèi)和接口去掉
                continue;
            }
            Constructor<?>[] dcs = clazz.getDeclaredConstructors();
            boolean noArgsConst = false;
            for(Constructor<?> c : dcs){
                if(c.getParameterTypes().length == 0 && Modifier.isPublic(c.getModifiers())){
                    noArgsConst = true;
                    break;
                }
            }
            if(!noArgsConst){//沒(méi)有public的無(wú)參構(gòu)造方法的去掉
                continue;
            }
            Object object = clazz.newInstance();
            MAP.put(clazz, object);
        }
    }
    /**
     * 獲取包下面的說(shuō)有類(lèi)名
     * @param packageName
     * @return
     * @throws IOException
     */
    private static List<String> classNames(String packageName)throws IOException {
        String packageDirName = packageName.replace('.', '/');
        Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
        List<String> classes = new ArrayList<String>();
        while(resources.hasMoreElements()){
            URL url = resources.nextElement();
            String protocol = url.getProtocol();
            if("jar".equals(protocol)){//項(xiàng)目引用的第三方j(luò)ar文件
                JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
                classesFromJar(jar, packageName, classes);
            }
            if("file".equals(protocol)){//項(xiàng)目自身pachage的.class文件
                String packagePath = URLDecoder.decode(url.getFile(), "UTF-8");
                classesFromFile(packagePath,packageName, classes);
            }
        }
        return classes;
    }
    /**
     * 從自身pachage中的.class獲取類(lèi)名
     * @param packagePath
     * @param packageName
     * @param classes
     */
    private static void classesFromFile(String packagePath,String packageName,List<String> classes){
        File dir = new File(packagePath);
        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }
        File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() || pathname.getName().endsWith(".class");
            }
        });
        for(File file : files){
            if(file.isDirectory()){
                classesFromFile(file.getAbsolutePath(), packageName + "." + file.getName(), classes);
            }else{
                classes.add(packageName + "." + file.getName().replace(".class", ""));
            }
        }
    }
    /**
     * 從引用的第三方j(luò)ar中的.class獲取類(lèi)名
     * @param jar
     * @param packageName
     * @param classes
     */
    private static void classesFromJar(JarFile jar,String packageName,List<String> classes){
        Enumeration<JarEntry> entries = jar.entries();
        while(entries.hasMoreElements()){
            JarEntry entry = entries.nextElement();
            String name = entry.getName();
            if(!name.replace("/", ".").startsWith(packageName) || !name.endsWith(".class")){
                continue;
            }
            classes.add(name.replace("/", ".").replace(".class", ""));
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容