Java Code Examples for org.springframework.context.ConfigurableApplicationContext#refresh()
The following examples show how to use
org.springframework.context.ConfigurableApplicationContext#refresh() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringBeanLocatorTest.java From sakai with Educational Community License v2.0 | 6 votes |
@Test public void testWait() throws InterruptedException { ConfigurableApplicationContext context = new GenericApplicationContext(); ConfigurableListableBeanFactory factory = context.getBeanFactory(); Object bean = new Object(); factory.registerSingleton("test", bean); context.refresh(); Locator get1 = new Locator(SpringBeanLocator.getInstance()); Locator get2 = new Locator(SpringBeanLocator.getInstance()); get1.start(); get2.start(); // Increase the chances that the other threads will have run Thread.yield(); SpringBeanLocator.setApplicationContext(context); get1.join(10000); get2.join(10000); // Cross thread assertions Assert.assertTrue(get1.good); Assert.assertTrue(get2.good); }
Example 2
Source File: DelegatingFilterProxy.java From spring-analysis-note with MIT License | 6 votes |
/** * Return the {@code WebApplicationContext} passed in at construction time, if available. * Otherwise, attempt to retrieve a {@code WebApplicationContext} from the * {@code ServletContext} attribute with the {@linkplain #setContextAttribute * configured name} if set. Otherwise look up a {@code WebApplicationContext} under * the well-known "root" application context attribute. The * {@code WebApplicationContext} must have already been loaded and stored in the * {@code ServletContext} before this filter gets initialized (or invoked). * <p>Subclasses may override this method to provide a different * {@code WebApplicationContext} retrieval strategy. * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not found * @see #DelegatingFilterProxy(String, WebApplicationContext) * @see #getContextAttribute() * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext) * @see WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ @Nullable protected WebApplicationContext findWebApplicationContext() { if (this.webApplicationContext != null) { // The user has injected a context at construction time -> use it... if (this.webApplicationContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) this.webApplicationContext; if (!cac.isActive()) { // The context has not yet been refreshed -> do so before returning it... cac.refresh(); } } return this.webApplicationContext; } String attrName = getContextAttribute(); if (attrName != null) { return WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName); } else { return WebApplicationContextUtils.findWebApplicationContext(getServletContext()); } }
Example 3
Source File: DelegatingFilterProxy.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Return the {@code WebApplicationContext} passed in at construction time, if available. * Otherwise, attempt to retrieve a {@code WebApplicationContext} from the * {@code ServletContext} attribute with the {@linkplain #setContextAttribute * configured name} if set. Otherwise look up a {@code WebApplicationContext} under * the well-known "root" application context attribute. The * {@code WebApplicationContext} must have already been loaded and stored in the * {@code ServletContext} before this filter gets initialized (or invoked). * <p>Subclasses may override this method to provide a different * {@code WebApplicationContext} retrieval strategy. * @return the {@code WebApplicationContext} for this proxy, or {@code null} if not found * @see #DelegatingFilterProxy(String, WebApplicationContext) * @see #getContextAttribute() * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext) * @see WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE */ protected WebApplicationContext findWebApplicationContext() { if (this.webApplicationContext != null) { // The user has injected a context at construction time -> use it... if (this.webApplicationContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) this.webApplicationContext; if (!cac.isActive()) { // The context has not yet been refreshed -> do so before returning it... cac.refresh(); } } return this.webApplicationContext; } String attrName = getContextAttribute(); if (attrName != null) { return WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName); } else { return WebApplicationContextUtils.findWebApplicationContext(getServletContext()); } }
Example 4
Source File: EnvironmentSystemIntegrationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 5
Source File: EnvironmentSystemIntegrationTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 6
Source File: BeanFactoryPostProcessorBootstrap.java From spring-analysis-note with MIT License | 6 votes |
public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("factory.bean/factory-post-processor.xml"); BeanFactoryPostProcessor beanFactoryPostProcessor = (BeanFactoryPostProcessor) context.getBean("carPostProcessor"); beanFactoryPostProcessor.postProcessBeanFactory(context.getBeanFactory()); // 输出 :Car{maxSpeed=0, brand='*****', price=10000.0},敏感词被替换了 System.out.println(context.getBean("car")); // 硬编码 后处理器执行时间 BeanFactoryPostProcessor hardCodeBeanFactoryPostProcessor = new HardCodeBeanFactoryPostProcessor(); context.addBeanFactoryPostProcessor(hardCodeBeanFactoryPostProcessor); // 更新上下文 context.refresh(); // 输出 : // Hard Code BeanFactory Post Processor execute time // Car{maxSpeed=0, brand='*****', price=10000.0} System.out.println(context.getBean("car")); }
Example 7
Source File: EnvironmentIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void repro() { ConfigurableApplicationContext parent = new GenericApplicationContext(); parent.refresh(); AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext(); child.setParent(parent); child.refresh(); ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class); assertThat("unknown env", env, anyOf( sameInstance(parent.getEnvironment()), sameInstance(child.getEnvironment()))); assertThat("expected child ctx env", env, sameInstance(child.getEnvironment())); child.close(); parent.close(); }
Example 8
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 9
Source File: EnvironmentSystemIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void genericApplicationContext_standardEnv() { ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); ctx.refresh(); assertHasStandardEnvironment(ctx); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); }
Example 10
Source File: SerializableBeanFactoryMemoryLeakTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void assertFactoryCountThroughoutLifecycle(ConfigurableApplicationContext ctx) throws Exception { assertThat(serializableFactoryCount(), equalTo(0)); try { ctx.refresh(); assertThat(serializableFactoryCount(), equalTo(1)); ctx.close(); } catch (BeanCreationException ex) { // ignore - this is expected on refresh() for failure case tests } finally { assertThat(serializableFactoryCount(), equalTo(0)); } }
Example 11
Source File: ApplicationListenerOnSpringEventsBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // 创建 ConfigurableApplicationContext 实例 GenericApplicationContext ConfigurableApplicationContext context = new GenericApplicationContext(); System.out.println("创建 Spring 应用上下文 : " + context.getDisplayName()); // 添加 ApplicationListener 非泛型实现 context.addApplicationListener(event -> System.out.println(event.getClass().getSimpleName()) ); // refresh() : 初始化应用上下文 System.out.println("应用上下文准备初始化..."); context.refresh(); // 发布 ContextRefreshedEvent System.out.println("应用上下文已初始化..."); // stop() : 停止应用上下文 System.out.println("应用上下文准备停止启动..."); context.stop(); // 发布 ContextStoppedEvent System.out.println("应用上下文已停止启动..."); // start(): 启动应用上下文 System.out.println("应用上下文准备启动启动..."); context.start(); // 发布 ContextStartedEvent System.out.println("应用上下文已启动启动..."); // close() : 关闭应用上下文 System.out.println("应用上下文准备关闭..."); context.close(); // 发布 ContextClosedEvent System.out.println("应用上下文已关闭..."); }
Example 12
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
Example 13
Source File: EnvironmentSystemIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void genericApplicationContext_standardEnv() { ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); ctx.refresh(); assertHasStandardEnvironment(ctx); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); }
Example 14
Source File: ApplicationInitializer.java From org.openwms with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} * <p> * Depending on the underlying platform, different Spring profiles are included. */ @Override public void initialize(ConfigurableApplicationContext applicationContext) { String activeProfile = System.getProperty("spring.profiles.active"); if (SpringProfiles.OSGI.equalsIgnoreCase(activeProfile)) { LOGGER.info("Running in OSGI environment"); } else if (SpringProfiles.NON_OSGI.equalsIgnoreCase(activeProfile)) { LOGGER.info("Running in a non OSGI environment"); } else { applicationContext.getEnvironment().setActiveProfiles(SpringProfiles.NON_OSGI); applicationContext.refresh(); LOGGER.info("Switched to a non OSGI environment"); } }
Example 15
Source File: AbstractReactiveWebInitializer.java From java-technology-stack with MIT License | 5 votes |
/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
Example 16
Source File: SerializableBeanFactoryMemoryLeakTests.java From java-technology-stack with MIT License | 5 votes |
private void assertFactoryCountThroughoutLifecycle(ConfigurableApplicationContext ctx) throws Exception { assertThat(serializableFactoryCount(), equalTo(0)); try { ctx.refresh(); assertThat(serializableFactoryCount(), equalTo(1)); ctx.close(); } catch (BeanCreationException ex) { // ignore - this is expected on refresh() for failure case tests } finally { assertThat(serializableFactoryCount(), equalTo(0)); } }
Example 17
Source File: EnvironmentSystemIntegrationTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void genericApplicationContext_standardEnv() { ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); ctx.refresh(); assertHasStandardEnvironment(ctx); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); }
Example 18
Source File: AbstractReactiveWebInitializer.java From spring-analysis-note with MIT License | 5 votes |
/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
Example 19
Source File: MSF4JSpringApplication.java From msf4j with Apache License 2.0 | 5 votes |
public static ConfigurableApplicationContext run(Class sources, String... args) { MSF4JSpringApplication application = new MSF4JSpringApplication(sources); ConfigurableApplicationContext context = application.run(false, args); application.applyInitializers(context); context.refresh(); return context; }
Example 20
Source File: TestComponentManagerContainer.java From sakai with Educational Community License v2.0 | 4 votes |
/** * create a component manager based on a list of component.xml * @param configPaths a ';' seperated list of xml bean config files * @throws IOException */ public TestComponentManagerContainer(String configPaths, Properties props) throws IOException { // we assume that all the jars are in the same classloader, so this will // not check for // incorrect bindings and will not fully replicate the tomcat // experience, but is an easier environment // to work within for kernel testing. // For a more complex structure we could use the kernel poms in the repo // to generate the dep list. if ( ComponentManager.m_componentManager != null ) { log.info("Closing existing Component Manager "); /* try { ComponentManager.m_componentManager.close(); } catch ( Throwable t ) { log.warn("Close Failed with message, safe to ignore "+t.getMessage()); } */ log.info("Closing Complete "); ComponentManager.m_componentManager = null; } log.info("Starting Component Manager with ["+configPaths+"]"); ComponentManager.setLateRefresh(true); componentManager = (SpringCompMgr) ComponentManager.getInstance(); ConfigurableApplicationContext ac = componentManager.getApplicationContext(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Supply any additional configuration. if (props != null) { PropertyOverrideConfigurer beanFactoryPostProcessor = new PropertyOverrideConfigurer(); beanFactoryPostProcessor.setBeanNameSeparator("@"); beanFactoryPostProcessor.setProperties(props); ac.addBeanFactoryPostProcessor(beanFactoryPostProcessor); } // we could take the kernel bootstrap from from the classpath in future // rather than from // the filesystem List<Resource> config = new ArrayList<Resource>(); String[] configPath = configPaths.split(";"); for ( String p : configPath) { File xml = new File(p); config.add(new FileSystemResource(xml.getCanonicalPath())); } loadComponent(ac, config, classLoader); ac.refresh(); // SAK-20908 - band-aid for TLM sync issues causing tests to fail // This sleep shouldn't be needed but it seems these tests are starting before ThreadLocalManager has finished its startup. try { Thread.sleep(500); // 1/2 second log.debug("Finished starting the component manager"); } catch (InterruptedException e) { log.error("Component manager startup interrupted..."); } }