package com.ag.secp.platform.spring;

import com.ag.util.StrUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Created by shaoqiang on 16/7/13.
 */
public class SpringApplicationContext implements ApplicationContextAware {

    private static ApplicationContext appContext;

    // Private constructor prevents instantiation from other classes
    private SpringApplicationContext() {}


    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        appContext = applicationContext;

    }

    public static Object getBean(String beanName) {
        if (appContext.containsBeanDefinition(StrUtil.firstToLower(beanName))) {
            return appContext.getBean(StrUtil.firstToLower(beanName));
        }else {
            return appContext.getBean(StrUtil.firstToUpper(beanName));
        }
    }

    public static boolean isContainsMain(String beanName){
        return appContext.containsBeanDefinition(StrUtil.firstToLower(beanName)) || appContext.containsBeanDefinition(StrUtil.firstToUpper(beanName));
    }

}