接口动态调用

spring配置文件配置

<"bean id="dt" class="DtClass" />

调用方法

private ServiceOutput doClazzByBeanName(String beanName, ServiceInput input) throws ServiceException {

    ServiceOutput output = new CServiceOutput(input);
    try {
        ApplicationContext context = ApplicationContextUtil.getContext();
        IServcieV2 service = (Servcie) context.getBean(beanName);
        output = service.doService(input);
    } catch (ServiceException e) {
        throw new ServiceException(ResultType.ErrorTip, "动态调用服务" + beanName + "异常");
    } catch (Exception e) {
        throw new ServiceException(ResultType.ErrorTip, "动态获取服务" + beanName + "异常");
    }
    return output;
}

ApplicationContextUtil类

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

public class ApplicationContextUtil implements ApplicationContextAware {

private static ApplicationContext context;

public ApplicationContextUtil() {
}

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

public static ApplicationContext getContext() {
    return context;
}

}

上一篇:技术栈累积


下一篇:Java关键字transient和volatile