package com.ag.base;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/3/23 0023.
 */
@Component
public class Reflect implements ApplicationContextAware {
    private static Reflect reflect = null;
    private static ApplicationContext applicationContext;


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

    private Reflect(){

    }
    public static Reflect newInstance(){
        if(reflect == null){
            reflect = new Reflect();
        }
        return reflect;
    }

    public Object getBeanById(String id){
        return applicationContext.getBean(id);
    }

    /*private Object execute(final String clsPath, final String methodName, HttpServletRequest request, HttpServletResponse response) {
        String methodN = methodName;
        if(StringUtils.isEmpty(methodName)){
            methodN = "execute";
        }
        //System.out.println("clsPath::"+clsPath);
        try {
            Class<?> clz = Class.forName(clsPath);
            Object obj = clz.newInstance();
            Method m = clz.getMethod(methodN, HttpServletRequest.class, HttpServletResponse.class);
            return m.invoke(obj, request, response);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;

    }*/

    public Object execute(final String beanId) {
        //System.out.println("clsPath::"+clsPath);
        /*try {
            Class<?> clz = Class.forName(clsPath);
            Object obj = clz.newInstance();
            return obj;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;*/
        return applicationContext.getBean(beanId);
    }

}
