Search in sources :

Example 1 with ContextCleanupListener

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

the class WebApplicationContextScopeTests method testApplicationScope.

@Test
public void testApplicationScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
    assertThat(ac.getServletContext().getAttribute(NAME)).isNull();
    DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
    assertThat(ac.getServletContext().getAttribute(NAME)).isSameAs(bean);
    assertThat(ac.getBean(NAME)).isSameAs(bean);
    new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
    assertThat(bean.wasDestroyed()).isTrue();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) ContextCleanupListener(org.springframework.web.context.ContextCleanupListener) ServletContextEvent(jakarta.servlet.ServletContextEvent) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContextEvent (jakarta.servlet.ServletContextEvent)1 Test (org.junit.jupiter.api.Test)1 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)1 ContextCleanupListener (org.springframework.web.context.ContextCleanupListener)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)1