Search in sources :

Example 26 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class ImportsContextCustomizerFactoryWithAutoConfigurationTests method testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext.

@Test
public void testClassesThatOnlyHaveDifferingUnrelatedAnnotationsShareAContext() throws InitializationError {
    RunNotifier notifier = new RunNotifier();
    new SpringJUnit4ClassRunner(DataJpaTest1.class).run(notifier);
    ApplicationContext test1Context = contextFromTest;
    new SpringJUnit4ClassRunner(DataJpaTest2.class).run(notifier);
    ApplicationContext test2Context = contextFromTest;
    assertThat(test1Context).isSameAs(test2Context);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) RunNotifier(org.junit.runner.notification.RunNotifier) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 27 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-boot by spring-projects.

the class SpringBootServletInitializer method createRootApplicationContext.

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.listeners(new ServletContextApplicationListener(servletContext));
    builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty() && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(!application.getSources().isEmpty(), "No SpringApplication sources have been defined. Either override the " + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilterConfiguration.class);
    }
    return run(application);
}
Also used : AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ParentContextApplicationContextInitializer(org.springframework.boot.builder.ParentContextApplicationContextInitializer)

Example 28 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-framework by spring-projects.

the class UnsupportedInterceptor method testWithDependencyChecking.

@Test
@SuppressWarnings("resource")
public void testWithDependencyChecking() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass());
    ctx.getBean("testBean");
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test)

Example 29 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-framework by spring-projects.

the class TestBean method explicitConfigClassBeanNameIsRespected.

/**
	 * Tests that specifying @Configuration(value="foo") results in registering
	 * the configuration class with bean name 'foo'.
	 */
@Test
public void explicitConfigClassBeanNameIsRespected() {
    ApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithCustomName.class);
    // attempt to retrieve the instance by its specified name
    ConfigWithCustomName configObject = (ConfigWithCustomName) context.getBean("customConfigBeanName");
    assertNotNull(configObject);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 30 with ApplicationContext

use of org.springframework.context.ApplicationContext in project spring-framework by spring-projects.

the class AutoProxyLazyInitTests method withNonStaticBeanMethodAndInterface.

@Test
public void withNonStaticBeanMethodAndInterface() {
    MyBeanImpl.initialized = false;
    ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
    MyBean bean = ctx.getBean("myBean", MyBean.class);
    assertFalse(MyBeanImpl.initialized);
    bean.doIt();
    assertTrue(MyBeanImpl.initialized);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Aggregations

ApplicationContext (org.springframework.context.ApplicationContext)532 Test (org.junit.Test)256 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)161 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)38 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)32 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)28 File (java.io.File)24 Messenger (org.springframework.scripting.Messenger)24 DataSource (javax.sql.DataSource)23 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)21 Refreshable (org.springframework.aop.target.dynamic.Refreshable)20 StubCloudConnectorTest (org.springframework.cloud.StubCloudConnectorTest)17 SchedulerException (org.quartz.SchedulerException)16 HashMap (java.util.HashMap)15 Map (java.util.Map)14 WebApplicationContext (org.springframework.web.context.WebApplicationContext)14 MovieMapper (com.mapper.MovieMapper)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)13 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)13 ArrayList (java.util.ArrayList)12