-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
刘义
committed
Oct 25, 2018
1 parent
9f3535c
commit 238c0df
Showing
7 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
framework/src/main/java/com/custom/framework/annotation/Action.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
17 changes: 17 additions & 0 deletions
17
framework/src/main/java/com/custom/framework/annotation/Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
17 changes: 17 additions & 0 deletions
17
framework/src/main/java/com/custom/framework/annotation/Inject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
18 changes: 18 additions & 0 deletions
18
framework/src/main/java/com/custom/framework/annotation/Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
75 changes: 75 additions & 0 deletions
75
framework/src/main/java/com/custom/framework/helper/ClassHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
framework/src/main/java/com/custom/framework/util/ClassUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |