java web 加载类_Java Web框架 基础知识 类加载 动态加载 jar class

这篇博客介绍了如何使用Java的类加载器、JarURLConnection和JarFile等工具,遍历文件系统和JAR文件,获取指定包名下的所有类。内容包括静态方法的实现,如获取类加载器、加载类以及获取类集合,便于在实际开发中进行类的管理和操作。
摘要由CSDN通过智能技术生成

1、获取指定包名下的所有类。如JarURLConnection、JarFile的用法。

2、ClassUtil.java

package mvc.framework.util;

import java.io.File;

import java.io.FileFilter;

import java.net.JarURLConnection;

import java.net.URL;

import java.util.Enumeration;

import java.util.HashSet;

import java.util.Set;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

// 类操作工具类

public final class ClassUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class);

// 获取类加载器

public static ClassLoader getClassLoader() {

return Thread.currentThread().getContextClassLoader();

}

// 加载类

public static Class> loadClass(String className, boolean isInitialized) {

Class> cls;

try {

cls = Class.forName(className, isInitialized, getClassLoader());

} catch (ClassNotFoundException e) {

LOGGER.error("load class failure", e);

throw new RuntimeException(e);

}

return cls;

}

// 加载类(默认将初始化类)

public static Class> loadClass(String className) {

return loadClass(className, true);

}

// 获取指定包名下的所有类

public static Set> getClassSet(String packageName) {

Set> classSet = new HashSet>();

try {

Enumeration urls = getClassLoader().getResources(packageName.replace(".", "/"));

while (urls.hasMoreElements()) {

URL url = urls.nextElement();

if (url != null) {

String protocol = url.getProtocol();

if (protocol.equals("file")) {

String packagePath = url.getPath().replaceAll("%20", " ");

findClass(classSet, packagePath, packageName);

} else if (protocol.equals("jar")) {

JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();

if (jarURLConnection != null) {

JarFile jarFile = jarURLConnection.getJarFile();

if (jarFile != null) {

Enumeration jarEntries = jarFile.entries();

while (jarEntries.hasMoreElements()) {

JarEntry jarEntry = jarEntries.nextElement();

String jarEntryName = jarEntry.getName();

if (jarEntryName.endsWith(".class")) {

String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/", ".");

addClass(classSet, className);

}

}

}

}

}

}

}

} catch (Exception e) {

LOGGER.error("get class set failure", e);

throw new RuntimeException(e);

}

return classSet;

}

// 查找类

private static void findClass(Set> classSet, String packagePath, String packageName) {

File[] files = new File(packagePath).listFiles(new FileFilter() {

public boolean accept(File file) {

return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();

}

});

for (File file : files) {

String fileName = file.getName();

if (file.isFile()) {

String className = fileName.substring(0, fileName.lastIndexOf("."));

if (StringUtil.isNotEmpty(packageName)) {

className = packageName + "." + className;

}

addClass(classSet, className);

} else {

String subPackagePath = fileName;

if (StringUtil.isNotEmpty(packagePath)) {

subPackagePath = packagePath + "/" + subPackagePath;

}

String subPackageName = fileName;

if (StringUtil.isNotEmpty(packageName)) {

subPackageName = packageName + "." + subPackageName;

}

findClass(classSet, subPackagePath, subPackageName);

}

}

}

// 把查找到的类加入集合

private static void addClass(Set> classSet, String className) {

Class> cls = loadClass(className, false);

classSet.add(cls);

}

// 测试

public static void main(String[] args) {

// file

Set> clsPackage = ClassUtil.getClassSet("mvc.web");

for (Class clazz : clsPackage) {

System.out.println(clazz.getName());

}

// jar

Set> clsJar = ClassUtil.getClassSet("org.apache.commons.io");

for (Class clazz : clsJar) {

System.out.println(clazz.getName());

}

/*

try {

URL classes_path = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();

String lib_path = classes_path.toString().replace("classes", "lib");

URL url = new URL("jar:" + lib_path + "commons-io-2.4.jar!/");

System.out.println(url.toString());

JarURLConnection conn = (JarURLConnection) url.openConnection();

JarFile jarFile = conn.getJarFile();

if (jarFile != null) {

Enumeration jarEntries = jarFile.entries();

while (jarEntries.hasMoreElements()) {

JarEntry jarEntry = jarEntries.nextElement();

String jarEntryName = jarEntry.getName();

if (jarEntryName.endsWith(".class")) {

String className = jarEntryName.substring(0, jarEntryName.lastIndexOf(".")).replaceAll("/",

".");

System.out.println(className);

}

}

}

} catch (IOException e) {

}

*/

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值