package com.hzya.frame.web.action;

import com.hzya.frame.basedao.service.BaseDaoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-05 9:40
 * @Version 1.0
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    Logger logger = LoggerFactory.getLogger(ApplicationContextUtil.class);
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
        return context;
    }

    public static BaseDaoUtil getBaseDaoUtil(){
        BaseDaoUtil baseDaoUtil = (BaseDaoUtil) ApplicationContextUtil.getBeanByName("baseDaoUtilImpl");
        return baseDaoUtil;
    }
    /**
     * 通过name获取 Bean
     * @param name beanName
     * @return Object
     */
    public static Object getBeanByName(String name){
        return getApplicationContext().getBean(name);
    }
    public static <T> Map<String, T> getBeansOfType(Class  beaNameClass){
        return getApplicationContext().getBeansOfType(beaNameClass);
    }

    public static <T> T getBeanByClass(Class<T> requiredType) throws BeansException{
        return getApplicationContext().getBean(requiredType);
    }

    public static Object getBeanByName2(String name){
        return getApplicationContext().getParentBeanFactory();
    }
}
