List of usage examples for org.springframework.context ConfigurableApplicationContext getBeanFactory
ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
From source file:net.firejack.platform.model.config.listener.ConfigContextLoaderListener.java
@Override public void start() { super.contextInitialized(event); ConfigurableApplicationContext context = (ConfigurableApplicationContext) getCurrentWebApplicationContext(); AnnotationTransactionAttributeSource source = context.getBean(AnnotationTransactionAttributeSource.class); Map<Object, DefaultTransactionAttribute> map = ClassUtils.getProperty(source, "attributeCache"); Map<String, BeanDefinition> managers = context.getBean(HibernateFactoryBean.HIBERNATE_MANAGER, Map.class); for (Map.Entry<String, BeanDefinition> entry : managers.entrySet()) { DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory(); beanFactory.registerBeanDefinition(entry.getKey(), entry.getValue()); }/*w w w . jav a 2 s. co m*/ Map<Class, String> targets = context.getBean(HibernateFactoryBean.HIBERNATE_TARGET, Map.class); Field targetField = null; for (Map.Entry<Object, DefaultTransactionAttribute> entry : map.entrySet()) { Object key = entry.getKey(); if (targetField == null) targetField = ClassUtils.getField(key.getClass(), "targetClass"); Class targetClass = ClassUtils.getProperty(key, targetField); String name = targets.get(targetClass); if (name != null) entry.getValue().setQualifier(name); } }
From source file:org.hx.rainbow.server.oc.monitor.service.MonitorService.java
public RainbowContext addDataBase(RainbowContext context) { try {//from w ww .j av a 2s .com ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) SpringApplicationContext .getApplicationContext(); DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext .getBeanFactory(); String serverName = (String) context.getAttr("serverName"); if (serverName == null || serverName.isEmpty()) { context.setMsg("????!"); } String url = (String) context.getAttr("url"); if (url == null || url.isEmpty()) { context.setMsg("url?!"); } String userName = (String) context.getAttr("userName"); if (userName == null || userName.isEmpty()) { context.setMsg("???!"); } String password = (String) context.getAttr("password"); if (password == null || password.isEmpty()) { context.setMsg("??!"); } beanFactory.registerBeanDefinition(serverName, getDefinition(serverName, url, userName, password)); context.setMsg("??!"); } catch (Exception ex) { ex.printStackTrace(); context.setMsg("?!" + ex.getMessage()); context.setSuccess(false); } return context; }
From source file:org.impalaframework.spring.module.loader.BaseApplicationContextLoader.java
private ConfigurableApplicationContext loadApplicationContext(Application application, ClassLoader classLoader, SpringModuleLoader moduleLoader, ApplicationContext parent, ModuleDefinition definition) { ClassLoader existing = ClassUtils.getDefaultClassLoader(); try {//from w w w.j a v a 2s . c om Thread.currentThread().setContextClassLoader(classLoader); if (logger.isDebugEnabled()) logger.debug("Setting class loader to " + classLoader); ConfigurableApplicationContext context = moduleLoader.newApplicationContext(application, parent, definition, classLoader); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); addBeanPostProcessors(application, definition, beanFactory); final String applicationId = application.getId(); BeanDefinitionReader reader = moduleLoader.newBeanDefinitionReader(applicationId, context, definition); if (reader != null) { //if this is null, then we assume moduleLoader or refresh takes care of this if (reader instanceof AbstractBeanDefinitionReader) { ((AbstractBeanDefinitionReader) reader).setBeanClassLoader(classLoader); } final Resource[] resources = moduleLoader.getSpringConfigResources(applicationId, definition, classLoader); reader.loadBeanDefinitions(resources); } moduleLoader.handleRefresh(applicationId, context, definition); return context; } finally { Thread.currentThread().setContextClassLoader(existing); } }
From source file:org.impalaframework.spring.service.bean.BaseExistingBeanExposingFactoryBean.java
/** * Finds the first parent {@link BeanFactory} which contains a bean of the given name *//*w ww . ja v a 2 s . com*/ BeanFactory findBeanFactory() { final ApplicationContext currentContext = maybeFindApplicationContext(); if (currentContext != null) { if (currentContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) currentContext; return configurableContext.getBeanFactory(); } else { logger.warn("Cannot return bean factory as application context is not an instance of " + ConfigurableApplicationContext.class.getName() + ": " + currentContext); } } throw new BeanDefinitionValidationException("No parent bean factory of application context [" + applicationContext.getDisplayName() + "] contains bean [" + getBeanNameToSearchFor() + "]"); }
From source file:org.mule.config.spring.SpringRegistry.java
private <T> T initialiseObject(ConfigurableApplicationContext applicationContext, String key, T object) throws LifecycleException { applicationContext.getBeanFactory().autowireBean(object); T initialised = (T) applicationContext.getBeanFactory().initializeBean(object, key); return initialised; }
From source file:org.sakaiproject.component.cover.TestComponentManagerContainer.java
/** * Load the application context using a single classloader * @param ac The spring application context * @param config a list of configurations represented as List of resources * @param loader the classloader to use// w w w . j av a2s.c om */ public void loadComponent(ConfigurableApplicationContext ac, List<Resource> config, ClassLoader loader) { ClassLoader current = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(loader); try { // make a reader XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader( (BeanDefinitionRegistry) ac.getBeanFactory()); // In Spring 2, classes aren't loaded during bean parsing unless // this // classloader property is set. reader.setBeanClassLoader(loader); reader.loadBeanDefinitions(config.toArray(new Resource[0])); } catch (Throwable t) { log.warn("loadComponentPackage: exception loading: " + config + " : " + t, t); } finally { // restore the context loader Thread.currentThread().setContextClassLoader(current); } }
From source file:org.sakaiproject.util.ComponentsLoader.java
/** * Load one component package into the AC * //from w w w.j a va2 s .c o m * @param dir * The file path to the component package * @param ac * The ApplicationContext to load into */ protected void loadComponentPackage(File dir, ConfigurableApplicationContext ac) { // setup the classloader onto the thread ClassLoader current = Thread.currentThread().getContextClassLoader(); ClassLoader loader = newPackageClassLoader(dir); M_log.info("loadComponentPackage: " + dir); Thread.currentThread().setContextClassLoader(loader); File xml = null; try { // load this xml file File webinf = new File(dir, "WEB-INF"); xml = new File(webinf, "components.xml"); // make a reader XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader( (BeanDefinitionRegistry) ac.getBeanFactory()); // In Spring 2, classes aren't loaded during bean parsing unless this // classloader property is set. reader.setBeanClassLoader(loader); List<Resource> beanDefList = new ArrayList<Resource>(); beanDefList.add(new FileSystemResource(xml.getCanonicalPath())); // Load the demo components, if necessary File demoXml = new File(webinf, "components-demo.xml"); if ("true".equalsIgnoreCase(System.getProperty("sakai.demo"))) { if (M_log.isDebugEnabled()) M_log.debug("Attempting to load demo components"); if (demoXml.exists()) { if (M_log.isInfoEnabled()) M_log.info("Loading demo components from " + dir); beanDefList.add(new FileSystemResource(demoXml.getCanonicalPath())); } } else { if (demoXml.exists()) { // Only log that we're skipping the demo components if they exist if (M_log.isInfoEnabled()) M_log.info("Skipping demo components from " + dir); } } if (overridesFolder != null) { File override = new File(overridesFolder, dir.getName() + ".xml"); if (override.isFile()) { beanDefList.add(new FileSystemResource(override.getCanonicalPath())); if (M_log.isInfoEnabled()) M_log.info("Overriding component definitions with " + override); } } reader.loadBeanDefinitions(beanDefList.toArray(new Resource[0])); } catch (Exception e) { M_log.error("loadComponentPackage: exception loading: " + xml + " : " + e, e); } finally { // restore the context loader Thread.currentThread().setContextClassLoader(current); } }
From source file:org.springframework.boot.diagnostics.FailureAnalyzers.java
private void prepareAnalyzer(ConfigurableApplicationContext context, FailureAnalyzer analyzer) { if (analyzer instanceof BeanFactoryAware) { ((BeanFactoryAware) analyzer).setBeanFactory(context.getBeanFactory()); }//from ww w. j a v a2 s .co m }
From source file:org.springframework.boot.SpringApplication.java
private ConfigurableApplicationContext doRun(SpringApplicationRunListeners listeners, String... args) { ConfigurableApplicationContext context; // Create and configure the environment ConfigurableEnvironment environment = getOrCreateEnvironment(); configureEnvironment(environment, args); listeners.environmentPrepared(environment); if (isWebEnvironment(environment) && !this.webEnvironment) { environment = convertToStandardEnvironment(environment); }//from w w w .j a v a 2 s. c o m if (this.bannerMode != Banner.Mode.OFF) { printBanner(environment); } // Create, load, refresh and run the ApplicationContext context = createApplicationContext(); context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments); // Load the sources Set<Object> sources = getSources(); Assert.notEmpty(sources, "Sources must not be empty"); load(context, sources.toArray(new Object[sources.size()])); listeners.contextLoaded(context); // Refresh the context refresh(context); if (this.registerShutdownHook) { try { context.registerShutdownHook(); } catch (AccessControlException ex) { // Not allowed in some environments. } } afterRefresh(context, applicationArguments); listeners.finished(context, null); return context; }
From source file:org.springframework.cloud.stream.binder.AbstractBinderTests.java
protected DirectChannel createBindableChannel(String channelName, BindingProperties bindingProperties) throws Exception { BindingServiceProperties bindingServiceProperties = new BindingServiceProperties(); bindingServiceProperties.getBindings().put(channelName, bindingProperties); ConfigurableApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh();//from w w w . j a v a 2 s.c om bindingServiceProperties.setApplicationContext(applicationContext); bindingServiceProperties.setConversionService(new DefaultConversionService()); bindingServiceProperties.afterPropertiesSet(); DirectChannel channel = new DirectChannel(); channel.setBeanName(channelName); MessageConverterConfigurer messageConverterConfigurer = new MessageConverterConfigurer( bindingServiceProperties, new CompositeMessageConverterFactory(null, null)); messageConverterConfigurer.setBeanFactory(applicationContext.getBeanFactory()); messageConverterConfigurer.afterPropertiesSet(); messageConverterConfigurer.configureOutputChannel(channel, channelName); return channel; }