Java tutorial
/** * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved. * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * http://www.ewcms.com */ package com.smhdemo.common.datasource.generate.factory.bean; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; 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.Service; import com.smhdemo.common.base.BaseRuntimeException; import com.smhdemo.common.datasource.entity.Base; import com.smhdemo.common.datasource.entity.Bean; import com.smhdemo.common.datasource.generate.factory.DataSourceFactoryable; import com.smhdemo.common.datasource.generate.service.DataSourceServiceable; /** * Bean?? * * @author ? */ @Service public class BeanDataSourceFactory implements DataSourceFactoryable, ApplicationContextAware { private static final Logger logger = LoggerFactory.getLogger(BeanDataSourceFactory.class); ApplicationContext ctx; public BeanDataSourceFactory() { super(); } @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { this.ctx = ctx; } @Override public DataSourceServiceable createService(Base alqcDataSource) { if (!(alqcDataSource instanceof Bean)) { logger.error("Bean?"); throw new BaseRuntimeException("Bean??", new Object[] { alqcDataSource.getClass() }); } Bean beanDataSource = (Bean) alqcDataSource; Object bean = ctx.getBean(beanDataSource.getBeanName()); if (bean == null) { logger.error("Bean???"); throw new BaseRuntimeException("Bean???", new Object[] { beanDataSource.getBeanName() }); } if (beanDataSource.getBeanMethod() == null) { if (!(bean instanceof DataSourceServiceable)) { logger.error("Bean???EwcmsDataSourceServiceable"); throw new BaseRuntimeException("Bean???EwcmsDataSourceServiceable", new Object[] { beanDataSource.getBeanName() }); } else { return (DataSourceServiceable) bean; } } else { Method serviceMethod; try { // serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(),null); // return (ReportDataSourceServiceable)serviceMethod.invoke(bean,null); serviceMethod = bean.getClass().getMethod(beanDataSource.getBeanMethod(), new Class[0]); return (DataSourceServiceable) serviceMethod.invoke(bean, new Object[0]); } catch (SecurityException e) { logger.error("SecurityException", e); throw new BaseRuntimeException(e); } catch (NoSuchMethodException e) { logger.error("bean??", e); throw new BaseRuntimeException("bean??", new Object[] { beanDataSource.getBeanName(), beanDataSource.getBeanMethod() }); } catch (IllegalArgumentException e) { logger.error("IllegalArgumentException", e); throw new BaseRuntimeException(e); } catch (IllegalAccessException e) { logger.error("IllegalAccessException", e); throw new BaseRuntimeException(e); } catch (InvocationTargetException e) { logger.error("InvocationTargetException", e.getMessage()); throw new BaseRuntimeException(e); } } } }