Skip to content

Commit

Permalink
添加类加载器相关类
Browse files Browse the repository at this point in the history
  • Loading branch information
刘义 committed Oct 25, 2018
1 parent 9f3535c commit 238c0df
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.custom.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 方法注解
* @author liuyi
* @date 2018/10/25
*/

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {

String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.custom.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 控制器注解
* @author liuyi
* @date 2018/10/25
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.custom.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 依赖注入注解
* @author liuyi
* @date 2018/10/25
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.custom.framework.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 服务类注解
* @author liuyi
* @date 2018/10/25
*/


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Service {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.custom.framework.helper;

import com.custom.framework.annotation.Controller;
import com.custom.framework.annotation.Service;
import com.custom.framework.util.ClassUtil;

import java.util.HashSet;
import java.util.Set;

/**
* 类操作助手类
* @author liuyi
* @date 2018/10/25
*/
public final class ClassHelper {

/**
* 定义类集合(用于存放所加载的类)
*/
private static final Set<Class<?>> CLASS_SET;

static {
String basePackage = ConfigHelper.getAppBasePackage() ;
CLASS_SET = ClassUtil.getClassSet(basePackage);
}

/**
* 获取应用包名下的所有类
* @return
*/
public static Set<Class<?>> getClassSet() {
return CLASS_SET;
}

/**
* 获取应用包下的所有service类
* @return
*/
public static Set<Class<?>> getServiceClassSet() {
Set<Class<?>> classSet = new HashSet<>();
for (Class<?> cls: CLASS_SET) {
if (cls.isAnnotationPresent((Service.class))) {
classSet.add(cls);
}
}
return classSet;
}

/**
* 获取应用包名下的所有controller类
* @return
*/
public static Set<Class<?>> getControllerClassSet() {
Set<Class<?>> classSet = new HashSet<>();
for (Class<?> cls : CLASS_SET) {
if (cls.isAnnotationPresent(Controller.class)) {
classSet.add(cls);
}
}
return classSet;
}

/**
* 获取应用包下所有的bean类(包括:Service。Controller 等)
* @return
*/
public static Set<Class<?>> getBeanClassSet() {
Set<Class<?>> beanClassSet = new HashSet<>();
beanClassSet.addAll(getControllerClassSet());
beanClassSet.addAll(getServiceClassSet());
return beanClassSet;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import java.util.ResourceBundle;

/**
* 属性文件助手类
* @author liuyi
* @date 2018/10/14
*/
public class ConfigHelper {
public final class ConfigHelper {

private static final ResourceBundle resource = PropsUtil.getResource(Constants.CONFIG_FILE);

Expand Down
122 changes: 122 additions & 0 deletions framework/src/main/java/com/custom/framework/util/ClassUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.custom.framework.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;


/**
* @author liuyi
* @date 2018/10/15
*/
public class ClassUtil {

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

private static final char DOT_CHAR = '.';

private static final char SLASH_CHAR = '/';

/**
* 获取类加载器
* @return
*/
public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}

/**
* 加载类
* @param className
* @param isInitialized
* @return
*/
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;
}

/**
* 获取指定包下的所有类
* @param packageName
* @return
*/
public static Set<Class<?>> getClassSet(String packageName) {
Set<Class<?>> classSet = new HashSet<>();
try {
Enumeration<URL> urls = getClassLoader().getResources(packageName.replace(DOT_CHAR, SLASH_CHAR));
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
if (url != null) {
String protocol = url.getProtocol();
if (protocol.equals("file")) {
String packagePath = url.getPath().replaceAll("%20", " ");
addClass(classSet, packagePath, packageName);
}
}
}
} catch (Exception e) {
LOGGER.error("get class set failure", e);
throw new RuntimeException(e);
}
return classSet;
}

/**
* 添加类
* @param classSet
* @param packagePath
* @param packageName
*/
private static void addClass(Set<Class<?>> classSet, String packagePath, String packageName) {
File[] files = new File(packagePath).listFiles(new FileFilter() {
@Override
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 (StringUtils.isNotEmpty(packageName)) {
className = packageName + "." + className;
}
doAddClass(classSet, className);
} else {
String subPackagePath = fileName;
if (StringUtils.isNotEmpty(packagePath)) {
subPackagePath = packagePath + "/" + subPackagePath;
}
String subPackageName = fileName;
if (StringUtils.isNotEmpty(packageName)) {
subPackageName = packageName + "/" + subPackageName;
}
addClass(classSet, subPackagePath, subPackageName);
}

}
}

/**
* 类放置到set
* @param classSet
* @param className
*/
private static void doAddClass(Set<Class<?>> classSet, String className) {
Class<?> cls = loadClass(className, false);
classSet.add(cls);
}
}

0 comments on commit 238c0df

Please sign in to comment.