/** 基于spring动态加载bean配置 */ public class DynamicBeanReaderImpl implements DynamicBeanReader, ApplicationContextAware { private static final Logger logger = LoggerFactory.getCustomerLog(DynamicBeanReaderImpl.class, "spring_dynamicBean"); private ConfigurableApplicationContext applicationContext = null; private XmlBeanDefinitionReader beanDefinitionReader; /*初始化方法*/ public void init() { beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) applicationContext.getBeanFactory()); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(applicationContext)); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (ConfigurableApplicationContext) applicationContext; } @Override public void loadBean(DynamicBean dynamicBean) { long startTime = System.currentTimeMillis(); String beanName = dynamicBean.getBeanName(); if (applicationContext.containsBean(beanName)) { logger.warn(beanName, "bean【{}】已经加载!"); // TODO 已经存在旧的bean,须不须要动态更新 return; } beanDefinitionReader.loadBeanDefinitions(new DynamicResource(dynamicBean)); logger.info( beanName, String.valueOf(System.currentTimeMillis() - startTime), "初始化bean【{}】耗时{}毫秒."); } }
public class StartServer implements Container { private static final Logger log = LoggerFactory.getApplicationLog(StartServer.class); public static final String DEFAULT_SPRING_CONFIG = "spring-all.xml"; static ClassPathXmlApplicationContext context; private static final String LOGNAME = "StartServer"; private static File HOME = null; private static String PLUGINHOME = null; public StartServer() {} @Override public void start() { log.info(LOGNAME, "{} starting ..."); getHome(); loadPluginsClass(); String[] configPath = getSpringConfigs(); context = new ClassPathXmlApplicationContext(configPath); context.start(); } private void loadPluginsClass() { String pluginHome = BusiPluginsHelper.getPluginHome(); PLUGINHOME = pluginHome; BusiPluginsHelper.loadPluginsClass(pluginHome); } public String[] getSpringConfigs() { final String[] pluginxms = BusiPluginsHelper.getPluginsConfig(PLUGINHOME); try { final String ppath = HOME.getCanonicalPath() + File.separator + "config"; SpringAll_xmlDefine define = new SpringAll_xmlDefine() { @Override protected void setCustomerXml(StringBuffer xmlBuf) { // setPropertyHolder("file:" + ppath + File.separator + "system.properties"); xmlBuf .append("<import resource=\"") .append("file:") .append(ppath) .append(File.separator); xmlBuf.append("spring-dao.xml\"/>"); xmlBuf .append("<import resource=\"") .append("file:") .append(ppath) .append(File.separator); xmlBuf.append("spring-services.xml\"/>"); if (pluginxms != null && pluginxms.length > 0) { for (String plugin : pluginxms) { xmlBuf.append("<import resource=\"").append(plugin).append("\"/>"); } } } }; define.setPropertyHolder("file:" + ppath + File.separator + "system.properties"); String springAllPath = ppath + File.separator + DEFAULT_SPRING_CONFIG; IOUtil.inputStream2File(IOUtil.str2InputStream(define.getXml()), springAllPath); File config = new File(springAllPath); if (config.exists() && config.isFile()) { return new String[] {"file:" + springAllPath}; } else { throw new AinbException(Constants.ERR_FRAME_NAME.getValue(), " get spring configs error."); } } catch (Exception e) { throw new AinbException(Constants.ERR_FRAME_NAME.getValue(), " get spring configs error.", e); } // if(pluginxms!=null && pluginxms.length>0){ // String[] configs = new String[pluginxms.length + 1]; // configs[0] = DEFAULT_SPRING_CONFIG; // System.arraycopy(pluginxms, 0, configs, 1, pluginxms.length); // // return configs; // } // return new String[]{DEFAULT_SPRING_CONFIG}; } @Override public void stop() { try { if (context != null) { context.stop(); context.close(); context = null; } log.info(LOGNAME, "{} stopped success."); } catch (Throwable e) { log.error("{} stopped error:{}", e, LOGNAME); } } public File getHome() { if (HOME == null) { String appHome = BusiPluginsHelper.getAppHome(); HOME = BusiPluginsHelper.verifyHome(appHome, NB_FRAME_CONFIG); } return HOME; } }