List of usage examples for org.springframework.context.support AbstractXmlApplicationContext getBean
@Override public Object getBean(String name) throws BeansException
From source file:soundsystem.CDPlayerMain.java
public static void main(String[] args) { AbstractXmlApplicationContext context = null; context = new FileSystemXmlApplicationContext("beans.xml"); //context = new ClassPathXmlApplicationContext("beans.xml"); CDPlayer cdPlayer = context.getBean(CDPlayer.class); cdPlayer.play();// www. jav a 2 s . c om }
From source file:il.ac.tau.yoavram.pes.SpringRunner.java
public static void run(String[] args) throws IOException { System.out.println("Starting " + SpringRunner.class.getSimpleName()); SimulationConfigurer configurer = new SimulationConfigurer(args); if (configurer.getSpringXmlConfig() == null) { System.err.println("Spring XML config file not defined"); System.err.println();// ww w . j av a2s .com System.exit(1); } if (configurer.getProperties() == null) { System.err.println("Properties not defined"); System.err.println(); System.exit(1); } // get the properties Properties properties = configurer.getProperties(); String jobName = properties.getProperty(SimulationConfigurer.JOB_NAME_KEY); // create context AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext(); // add properties to context logger.info("Adding properties to context: " + properties.toString()); PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); propertyPlaceholderConfigurer.setProperties(properties); context.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer); // set config location String configLocation = configurer.getSpringXmlConfig().toString(); logger.info("Loading context from file " + configLocation); context.setConfigLocation(configLocation); // make sure destroy methods will be called and refresh the context context.registerShutdownHook(); context.refresh(); // persist properties try { PropertiesPersister persister = context.getBean(PropertiesPersister.class); if (persister != null) { persister.persist(properties); } } catch (NoSuchBeanDefinitionException e) { // nothing to do } // get the simulation bean and run it Simulation simulation = context.getBean("simulation", Simulation.class); logger.debug("Starting simulation " + jobName); simulation.start(); }
From source file:com.taobao.ad.es.common.datasource.DataSourceFactory.java
public Connection getConnection(String contextPath, int dsType, String dataSource) throws BeansException, SQLException, NamingException { Connection connection = null; switch (dsType) { case JobData.JOBDATA_DATA_DATASOURCE_TYPE_DYNAMIC: // BeanFactoryLocator sysCtxLocator = // SingletonBeanFactoryLocator.getInstance(WebAgentConstants.DATASOURCE_CONTEXT); // BeanFactoryReference brf = // sysCtxLocator.useBeanFactory(WebAgentConstants.DATASOURCE_CONTEXT_KEY); // connection = ((DataSource) // brf.getFactory().getBean(dataSource)).getConnection(); AbstractXmlApplicationContext context = null; if (StringUtils.isEmpty(contextPath)) { context = new ClassPathXmlApplicationContext("classpath:es-agent-ds.xml"); } else {/* ww w. j a v a 2 s. c o m*/ context = new FileSystemXmlApplicationContext(contextPath); } connection = ((DataSource) context.getBean(dataSource)).getConnection(); break; default: Context cxt = new InitialContext(); Context envCtx = (Context) cxt.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/" + dataSource); connection = ds.getConnection(); break; } return connection; }
From source file:com.clican.pluto.dataprocess.engine.impl.ProcessorContainerImpl.java
/** * Springinit-method???//from w w w. j a v a 2 s .com */ @SuppressWarnings("unchecked") public void start() { if (log.isInfoEnabled()) { log.info("Begin to start Process Data Container"); } for (String scan : scanList) { scan = scan.trim(); try { String pattern1; String pattern2; if (scan.startsWith("file")) { pattern1 = scan + "/**/*.xml"; pattern2 = scan + "/**/*.properties"; } else { pattern1 = ClassUtils.convertClassNameToResourcePath(scan) + "/**/*.xml"; pattern2 = ClassUtils.convertClassNameToResourcePath(scan) + "/**/*.properties"; } Resource[] resources1 = resourcePatternResolver.getResources(pattern1); Resource[] resources2 = resourcePatternResolver.getResources(pattern2); Map<String, Resource> map1 = new HashMap<String, Resource>(); Map<String, Resource> map2 = new HashMap<String, Resource>(); Set<String> usingPropertyResource = new HashSet<String>(); for (Resource resource : resources1) { map1.put(resource.getFilename().split("\\.")[0], resource); } for (Resource resource : resources2) { String fileName = resource.getFilename().split("\\.")[0]; String[] fn = fileName.split("\\_"); if (fn.length == 2 && map1.containsKey(fn[0])) { usingPropertyResource.add(fn[0]); } else { throw new RuntimeException("properties[" + fileName + "]???"); } map2.put(resource.getFilename().split("\\.")[0], resource); } for (Resource resource1 : resources1) { final Resource xmlRes = resource1; String fileName1 = resource1.getFilename().split("\\.")[0]; if (usingPropertyResource.contains(fileName1)) { for (Resource resource2 : resources2) { AbstractXmlApplicationContext subContext = new AbstractXmlApplicationContext( this.applicationContext) { protected Resource[] getConfigResources() { return new Resource[] { xmlRes }; } }; String fileName2 = resource2.getFilename().split("\\.")[0]; String[] fn = fileName2.split("\\_"); if (fn[0].equals(fileName1)) { com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer parentConf = (com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer) applicationContext .getBean("propertyConfigurer"); Resource[] resources = new Resource[1 + parentConf.getLocations().length]; for (int i = 0; i < parentConf.getLocations().length; i++) { resources[i] = parentConf.getLocations()[i]; } resources[resources.length - 1] = resource2; com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer subContainerConf = new com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer(); subContainerConf.setFileEncoding("utf-8"); subContainerConf.setLocations(resources); subContext.addBeanFactoryPostProcessor(subContainerConf); subContext.refresh(); processorGroupSpringMap.put(fn[1], subContext); Collection<String> coll = (Collection<String>) subContext .getBeansOfType(BaseDataProcessor.class).keySet(); for (String beanName : coll) { BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName); if (bean.isStartProcessor()) { startProcessorNameMap.put(fn[1], beanName); break; } } if (!startProcessorNameMap.containsKey(fn[1])) { throw new RuntimeException( "?Process Container," + fn[1] + "?"); } } } } else { AbstractXmlApplicationContext subContext = new AbstractXmlApplicationContext( this.applicationContext) { protected Resource[] getConfigResources() { return new Resource[] { xmlRes }; } }; subContext.addBeanFactoryPostProcessor( (BeanFactoryPostProcessor) applicationContext.getBean("propertyConfigurer")); subContext.refresh(); processorGroupSpringMap.put(fileName1, subContext); Collection<String> coll = (Collection<String>) subContext .getBeansOfType(BaseDataProcessor.class).keySet(); for (String beanName : coll) { BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName); if (bean.isStartProcessor()) { startProcessorNameMap.put(fileName1, beanName); break; } } if (!startProcessorNameMap.containsKey(fileName1)) { throw new RuntimeException( "?Process Container," + fileName1 + "?"); } } } } catch (Exception e) { throw new RuntimeException(e); } } for (Deploy deploy : deployList) { try { ClassPathXmlApplicationContext subContext = new ClassPathXmlApplicationContext( new String[] { deploy.getUrl() }, this.applicationContext); if (StringUtils.isNotEmpty(deploy.getPropertyResources())) { com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer parentConf = (com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer) applicationContext .getBean("propertyConfigurer"); String[] propertyResources = deploy.getPropertyResources().split(","); Resource[] resources = new Resource[propertyResources.length + parentConf.getLocations().length]; for (int i = 0; i < parentConf.getLocations().length; i++) { resources[i] = parentConf.getLocations()[i]; } for (int i = parentConf.getLocations().length; i < resources.length; i++) { String propertyResource = propertyResources[i - parentConf.getLocations().length]; if (propertyResource.startsWith("classpath")) { resources[i] = new ClassPathResource( propertyResource.substring(propertyResource.indexOf(":") + 1)); } else { resources[i] = new FileSystemResource( propertyResource.substring(propertyResource.indexOf(":") + 1)); } } com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer subContainerConf = new com.clican.pluto.common.support.spring.PropertyPlaceholderConfigurer(); subContainerConf.setFileEncoding("utf-8"); subContainerConf.setLocations(resources); subContext.addBeanFactoryPostProcessor(subContainerConf); } else { subContext.addBeanFactoryPostProcessor( (BeanFactoryPostProcessor) applicationContext.getBean("propertyConfigurer")); } subContext.refresh(); processorGroupSpringMap.put(deploy.getName(), subContext); Collection<String> coll = (Collection<String>) subContext.getBeansOfType(BaseDataProcessor.class) .keySet(); for (String beanName : coll) { BaseDataProcessor bean = (BaseDataProcessor) subContext.getBean(beanName); if (bean.isStartProcessor()) { startProcessorNameMap.put(deploy.getName(), beanName); break; } } if (!startProcessorNameMap.containsKey(deploy.getName())) { throw new RuntimeException( "?Process Container," + deploy.getName() + "?"); } } catch (Exception e) { log.error("Depoly [" + deploy.getName() + "] failure", e); throw new RuntimeException(e); } } log.info("The Process Data Container has been started successfully."); }