一、注解操作的工具类,可以判断组合注解中是否包含某个注解
package syx.mvc.util;
import javax.annotation.*;
import java.lang.annotation.*;
public class AnnotationUtil {
public static boolean checkAnnotation(Class<?> clazz,Class<?> ann){
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() != Deprecated.class &&
annotation.annotationType() != SuppressWarnings.class &&
annotation.annotationType() != Override.class &&
annotation.annotationType() != PostConstruct.class &&
annotation.annotationType() != PreDestroy.class &&
annotation.annotationType() != Resource.class &&
annotation.annotationType() != Resources.class &&
annotation.annotationType() != Generated.class &&
annotation.annotationType() != Target.class &&
annotation.annotationType() != Retention.class &&
annotation.annotationType() != Documented.class &&
annotation.annotationType() != Inherited.class
) {
if (annotation.annotationType() == ann){
return true;
}else{
return checkAnnotation(annotation.annotationType(),ann);
}
}
}
return false;
}
}
二、填充IOC容器的工具类
package syx.mvc.util;
import syx.mvc.annotation.Component;
import syx.mvc.annotation.Scope;
import syx.mvc.config.BeanDefinition;
import java.util.HashMap;
import java.util.List;
public class IOCMapUtil {
public static HashMap<String, BeanDefinition> IOCMap(List<String> classNames, HashMap<String, BeanDefinition> ioc) {
classNames.forEach(e->{
try {
Class<?> clazz = AnnotationUtil.class.getClassLoader().loadClass(e);
boolean b = AnnotationUtil.checkAnnotation(clazz, Component.class);
if(b){
Component component = clazz.getAnnotation(Component.class);
BeanDefinition beanDefinition = generateBeanDefinition(clazz,component,e);
ioc.put(beanDefinition.getBeanName(),beanDefinition);
}
} catch (ClassNotFoundException exception) {
exception.printStackTrace();
}
});
return ioc;
}
private static BeanDefinition generateBeanDefinition(Class<?> clazz,Component component,String className){
String beanName;
String scopeType;
BeanDefinition beanDefinition = new BeanDefinition();
if(component == null){
String[] split = className.split("\\.");
beanName = split[split.length-1];
scopeType = "singleton";
}else{
if(component.value().isEmpty()){
String[] split = className.split("\\.");
beanName = split[split.length-1];
}else{
beanName = component.value();
}
Scope scope = clazz.getAnnotation(Scope.class);
if(scope != null){
scopeType = scope.value();
}else{
scopeType = "singleton";
}
}
beanDefinition.setBeanName(beanName);
beanDefinition.setBeanClass(clazz);
beanDefinition.setScope(scopeType);
return beanDefinition;
}
}
三、根据包名,加载该包下所有class文件的工具类
package syx.mvc.util;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class LoadFileUtil {
public static List<String> loadFiles(String packageName) {
if(packageName == null || packageName.isEmpty()){
throw new RuntimeException("包名不能为空");
}
String path = packageName.replaceAll("\\.", "/");
ClassLoader classLoader = LoadFileUtil.class.getClassLoader();
URL resource = classLoader.getResource(path);
File file = new File(resource.getFile());
return DFSLoadFile(file,"syx",".class");
}
private static List<String> DFSLoadFile(File file, String prefix, String suffix) {
List<String> classFiles = new ArrayList<>();
Stack<File> stack = new Stack<>();
stack.push(file);
while (!stack.isEmpty()) {
File f = stack.pop();
File[] files = f.listFiles();
for (File F : files) {
if(F.isDirectory()){
stack.push(F);
}else {
classFiles.add(replace(F.getAbsolutePath(),prefix,suffix));
}
}
}
return classFiles;
}
private static String replace(String target,String prefix,String suffix){
target = target.substring(target.indexOf(prefix), target.indexOf(suffix)).replace("\\", ".");
return target;
}
}
四、加载配置文件的工具类
package syx.mvc.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
public static Properties loadPropertoes(String fileName) throws IOException {
Properties properties = null;
InputStream inputStream = null;
try {
inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
if (inputStream == null){
throw new FileNotFoundException(fileName + "is not found");
}
properties = new Properties();
properties.load(inputStream);
}catch (IOException e){
logger.error("load properties failure",e);
}finally {
if (inputStream != null){
inputStream.close();
}
}
return properties;
}
public static String getString(Properties props, String key, String defaultValue) {
String value = defaultValue;
if (props.containsKey(key)) {
value = props.getProperty(key);
}
return value;
}
public static String getString(Properties props, String key) {
return getString(props, key, "");
}
public static int getInt(Properties props, String key) {
return getInt(props, key, 0);
}
public static int getInt(Properties props, String key, int defaultValue) {
int value = defaultValue;
if (props.containsKey(key)) {
value = Integer.parseInt(props.getProperty(key));
}
return value;
}
public static boolean getBoolean(Properties props, String key) {
return getBoolean(props, key, false);
}
public static boolean getBoolean(Properties props, String key, boolean defaultValue) {
boolean value = defaultValue;
if (props.containsKey(key)) {
value = Boolean.parseBoolean(props.getProperty(key));
}
return value;
}
}
五、反射工具类
package syx.mvc.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionUtil {
private static final Logger logger = LoggerFactory.getLogger(ReflectionUtil.class);
public static <T> T crtInstance(Class<T> clazz) {
T instance = null;
try {
instance = clazz.newInstance();
} catch (Exception e) {
logger.error("new instance failure", e);
e.printStackTrace();
}
return instance;
}
public static Object invokeMethod(Object object, Method method, Object... args) {
Object result = null;
try {
method.setAccessible(true);
result = method.invoke(object, args);
} catch (Exception e) {
logger.error("new instance failure", e);
e.printStackTrace();
}
return result;
}
public static void setField(Object obj, Field field, Object value) {
try {
field.setAccessible(true);
field.set(obj, value);
} catch (Exception e) {
logger.error("set field failure", e);
throw new RuntimeException(e);
}
}
}