com.ajaxjs.ioc.BeanContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajaxjs-base Show documentation
Show all versions of ajaxjs-base Show documentation
AJAXJS aims to full-stack, not only the server-side framework,
but also integrates the front-end library. It's written in HTML5 + Java, a successor to the JVM platform, efficient, secure, stable, cross-platform and many other advantages, but it abandoned the traditional enterprise architecture brought about by the large and bloated,
emphasizing the lightweight, and fast, very suitable for the Internet fast application.
/**
* 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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
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 getBeanByClass(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