Skip to content

Commit

Permalink
实现IOC和Bean容器功能
Browse files Browse the repository at this point in the history
  • Loading branch information
刘义 committed Oct 27, 2018
1 parent 238c0df commit 74d47a2
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.custom.framework.helper;

import com.custom.framework.util.ReflectionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Bean助手类
* @author liuyi
* @date 2018/10/25
*/
public class BeanHelper {

/**
* 定义Bean映射(用于存放Bean类与Bean实例的映射关系)
*/
private static final Map<Class<?>, Object> BEAN_MAP = new HashMap<Class<?>, Object>();

static {
Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
for (Class<?> beanClass : beanClassSet) {
Object obj = ReflectionUtil.newInstance(beanClass);
BEAN_MAP.put(beanClass, obj);
}
}

/**
* 获取Bean映射
* @return
*/
public static Map<Class<?>, Object> getBeanMap() {
return BEAN_MAP;
}

/**
* 获取Bean实例
* @param cls
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> cls) {
if (!BEAN_MAP.containsKey(cls)) {
throw new RuntimeException("can not get bean by class: " + cls);
}
return (T)BEAN_MAP.get(cls);
}

}
48 changes: 48 additions & 0 deletions framework/src/main/java/com/custom/framework/helper/IocHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.custom.framework.helper;


import com.custom.framework.annotation.Inject;
import com.custom.framework.util.ArrayUtil;
import com.custom.framework.util.CollectionUtil;
import com.custom.framework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
* 依赖注入助手类
* @author liuyi
* @date 2018/10/27
*/
public class IocHelper {

static {
//获取所有的Bean类与Bean实例之间的映射关系(简称BeanMap)
Map<Class<?>, Object> beanMap = BeanHelper.getBeanMap();
if (CollectionUtil.isNotEmpty(beanMap)) {
//遍历BeanMap
for (Map.Entry<Class<?>, Object> beanEntry : beanMap.entrySet()) {
//从BeanMap中获取Bean类与Bean实例
Class<?> beanClass = beanEntry.getKey();
Object beanInstance = beanEntry.getValue();
//获取Bean类定义的所有成员变量(简称Bean Field)
Field[] beanFields = beanClass.getDeclaredFields();
if (ArrayUtil.isNotEmpty(beanFields)) {
//遍历Bean Field
for (Field beanField : beanFields) {
//判断当前Bean Field 是否带有Inject注解
if (beanField.isAnnotationPresent(Inject.class)) {
//在Bean Map中获取Bean Field对应的实例
Class<?> beanFieldClass = beanField.getType();
Object beanFieldInstance = beanMap.get(beanFieldClass);
if (beanFieldInstance != null) {
//通过反射初始化 BeanField的值
ReflectionUtil.setField(beanInstance, beanField, beanFieldInstance);
}
}
}
}
}
}
}
}
28 changes: 28 additions & 0 deletions framework/src/main/java/com/custom/framework/util/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.custom.framework.util;

import org.apache.commons.lang3.ArrayUtils;

/**
* 数组工具类
* @author liuyi
* @date 2018/10/27
*/
public class ArrayUtil {
/**
* 判断数组是否非空
* @param array
* @return
*/
public static boolean isNotEmpty(Object[] array) {
return !ArrayUtils.isEmpty(array);
}

/**
* 判断数组是否为空
* @param array
* @return
*/
public static boolean isEmpty(Object[] array) {
return ArrayUtil.isEmpty(array);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.custom.framework.util;

import java.util.Map;
/**
* @author liuyi
* @date 2018/10/27
*/
public class CollectionUtil {

public static boolean isNotEmpty(Map<Class<?>, Object> map) {
if (map == null || map.size() == 0) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.custom.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* 反射工具类
* @author liuyi
* @date 2018/10/25
*/
public final class ReflectionUtil {

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

/**
* 创建实例
*/
public static Object newInstance(Class<?> cls) {
Object instance;
try {
instance = cls.newInstance();
} catch (Exception e) {
LOGGER.error("new instance failure", e);
throw new RuntimeException(e);
}
return instance;
}

/**
* 调用方法
*/
public static Object invokeMethod(Object obj, Method method, Object...args) {
Object result;
try {
method.setAccessible(true);
result = method.invoke(obj, args);
} catch (Exception e) {
LOGGER.error("invoke method failure", e);
throw new RuntimeException(e);
}
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);
}
}
}

0 comments on commit 74d47a2

Please sign in to comment.