All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smart4j.framework.ioc.BeanHelper Maven / Gradle / Ivy

There is a newer version: 2.3.2
Show newest version
package org.smart4j.framework.ioc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.smart4j.framework.aop.annotation.Aspect;
import org.smart4j.framework.core.ClassHelper;
import org.smart4j.framework.core.fault.InitializationError;
import org.smart4j.framework.ioc.annotation.Bean;
import org.smart4j.framework.mvc.annotation.Action;
import org.smart4j.framework.tx.annotation.Service;

/**
 * 初始化相关 Bean 类
 *
 * @author huangyong
 * @since 1.0
 */
public class BeanHelper {

    /**
     * Bean Map(Bean 类 => Bean 实例)
     */
    private static final Map, Object> beanMap = new HashMap, Object>();

    static {
        try {
            // 获取应用包路径下所有的类
            List> classList = ClassHelper.getClassList();
            for (Class cls : classList) {
                // 处理带有 Bean/Service/Action/Aspect 注解的类
                if (cls.isAnnotationPresent(Bean.class) ||
                    cls.isAnnotationPresent(Service.class) ||
                    cls.isAnnotationPresent(Action.class) ||
                    cls.isAnnotationPresent(Aspect.class)) {
                    // 创建 Bean 实例
                    Object beanInstance = cls.newInstance();
                    // 将 Bean 实例放入 Bean Map 中(键为 Bean 类,值为 Bean 实例)
                    beanMap.put(cls, beanInstance);
                }
            }
        } catch (Exception e) {
            throw new InitializationError("初始化 BeanHelper 出错!", e);
        }
    }

    /**
     * 获取 Bean Map
     */
    public static Map, Object> getBeanMap() {
        return beanMap;
    }

    /**
     * 获取 Bean 实例
     */
    @SuppressWarnings("unchecked")
    public static  T getBean(Class cls) {
        if (!beanMap.containsKey(cls)) {
            throw new RuntimeException("无法根据类名获取实例!" + cls);
        }
        return (T) beanMap.get(cls);
    }

    /**
     * 设置 Bean 实例
     */
    public static void setBean(Class cls, Object obj) {
        beanMap.put(cls, obj);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy