Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2015 Sp42 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ajaxjs.ioc;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import javax.inject.Inject;
import javax.inject.Named;
import com.ajaxjs.config.ConfigService;
import com.ajaxjs.ioc.aop.Aop;
import com.ajaxjs.util.CommonUtil;
import com.ajaxjs.util.ReflectUtil;
import com.ajaxjs.util.logger.LogHelper;
/**
* IOC 管理器,单例
*
* @author sp42 [email protected]
*/
public class BeanContext {
private static final LogHelper LOGGER = LogHelper.getLog(BeanContext.class);
/**
* 是否已初始化
*/
private static boolean isInitialized = false;
/**
* 存放对象
*/
public static Map beans = new HashMap<>();
/**
* 存放对象类
*/
public static Map> beansClz = new HashMap<>();
/**
* 记录依赖关系
*/
private static Map dependencies = new HashMap<>();
/**
* 获取对象
*
* @param id 对象标识
* @return 对象
*/
public static Object getBean(String id) {
return beans.get(id);
}
/**
* 按照类给出的 对象id(Bean 注解指定的 id) 查找实例
*
* @param clz 含有 Bean 注解的类对象
* @return 该类的实例,如果没有返回 null
*/
@SuppressWarnings("unchecked")
public static T getBean(Class clz) {
if (clz.getAnnotation(Bean.class) == null) {
IllegalArgumentException e = new IllegalArgumentException(
clz + " 这不是一个 ioc 的 bean。This is not a bean object that can be put into IOC.");
LOGGER.warning(e);
throw e;
}
// String name = clz.getAnnotation(Bean.class).value();
String name = getBeanId(clz.getAnnotation(Bean.class), clz);
Object obj = getBean(name);
return obj == null ? null : (T) obj;
}
/**
* 扫描注解、创建 bean 对象、记录依赖关系
*
* @param classes 扫描到的类集合
*/
public static void init(Set> classes) {
if (isInitialized)
LOGGER.warning("IOC 已经初始化。IOC System is already initialized.");
if (CommonUtil.isNull(classes)) {
LOGGER.warning("IOC 传入的类为空!请检查包名是否正确。 Non classes passed.");
return;
}
for (Class