@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.scopes != null) { this.scopes.forEach((scopeKey, value) -> { if (value instanceof Scope) { beanFactory.registerScope(scopeKey, (Scope) value); } else if (value instanceof Class) { Class<?> scopeClass = (Class<?>) value; Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); } else if (value instanceof String) { Class<?> scopeClass = ClassUtils.resolveClassName((String) value, this.beanClassLoader); Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); } else { throw new IllegalArgumentException("Mapped value [" + value + "] for scope key [" + scopeKey + "] is not an instance of required type [" + Scope.class.getName() + "] or a corresponding Class or String value indicating a Scope implementation"); } }); } }
public void restore() { this.scopes.forEach((key, value) -> { if (logger.isInfoEnabled()) { logger.info("Restoring user defined scope " + key); } this.beanFactory.registerScope(key, value); }); }
public class WebContextTestExecutionListener extends AbstractTestExecutionListener { @Override public void prepareTestInstance(TestContext testContext) { if (testContext.getApplicationContext() instanceof GenericApplicationContext) { GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); } } }
private void registerGlueCodeScope(ConfigurableApplicationContext context) { do { context.getBeanFactory().registerScope(GlueCodeScope.NAME, new GlueCodeScope()); context = (ConfigurableApplicationContext)context.getParent(); } while (context != null); } }
private void registerApplicationScope(ServletContext servletContext) { ServletContextScope appScope = new ServletContextScope(servletContext); getBeanFactory().registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope); // Register as ServletContext attribute, for ContextCleanupListener to detect it. servletContext.setAttribute(ServletContextScope.class.getName(), appScope); }
/** * Register web-specific scopes ("request", "session", "globalSession", "application") * with the given BeanFactory, as used by the WebApplicationContext. * @param beanFactory the BeanFactory to configure * @param sc the ServletContext that we're running within */ public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, @Nullable ServletContext sc) { beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope()); if (sc != null) { ServletContextScope appScope = new ServletContextScope(sc); beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope); // Register as ServletContext attribute, for ContextCleanupListener to detect it. sc.setAttribute(ServletContextScope.class.getName(), appScope); } beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory()); beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory()); beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory()); beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory()); if (jsfPresent) { FacesDependencyRegistrar.registerFacesDependencies(beanFactory); } }
@Test public void testSunnyDayWithBonaFideScopeInstance() { Scope scope = mock(Scope.class); factory.registerScope(FOO_SCOPE, scope); Map<String, Object> scopes = new HashMap<>(); scopes.put(FOO_SCOPE, scope); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); }
@Test public void withScopedProxyThroughRegex() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughRegex.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Test public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Test public void testDefaultScopedProxy() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyDefaultTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should not be a proxy assertFalse(AopUtils.isAopProxy(bean)); context.close(); }
@Test public void testNoScopedProxy() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyNoTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should not be a proxy assertFalse(AopUtils.isAopProxy(bean)); context.close(); }
@Test public void withScopedProxy() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxy.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); // test serializability assertThat(bean.foo(1), equalTo("bar")); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertThat(deserialized, notNullValue()); assertThat(deserialized.foo(1), equalTo("bar")); }
@Test public void testSpr10744() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getBeanFactory().registerScope("myTestScope", new MyTestScope()); context.register(MyTestConfiguration.class); context.refresh(); Foo bean1 = context.getBean("foo", Foo.class); Foo bean2 = context.getBean("foo", Foo.class); assertThat(bean1, sameInstance(bean2)); // Should not have invoked constructor for the proxy instance assertThat(createCount, equalTo(0)); assertThat(scopeCount, equalTo(0)); // Proxy mode should create new scoped object on each method call bean1.getMessage(); assertThat(createCount, equalTo(1)); assertThat(scopeCount, equalTo(1)); bean1.getMessage(); assertThat(createCount, equalTo(2)); assertThat(scopeCount, equalTo(2)); context.close(); }
@Test public void testScopedOverride() throws Exception { GenericApplicationContext ctx = new GenericApplicationContext(); new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT); SimpleMapScope scope = new SimpleMapScope(); ctx.getBeanFactory().registerScope("request", scope); ctx.refresh(); ITestBean bean = (ITestBean) ctx.getBean("testBean"); assertEquals("male", bean.getName()); assertEquals(99, bean.getAge()); assertTrue(scope.getMap().containsKey("scopedTarget.testBean")); assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass()); }
@Test public void testTargetClassScopedProxy() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyTargetClassTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should be a class-based proxy assertTrue(AopUtils.isCglibProxy(bean)); // test serializability assertEquals("bar", bean.foo(1)); ScopedProxyTestBean deserialized = (ScopedProxyTestBean) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertEquals("bar", deserialized.foo(1)); context.close(); }
@Test public void testInterfacesScopedProxy() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyInterfacesTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); // should cast to the interface FooService bean = (FooService) context.getBean("scopedProxyTestBean"); // should be dynamic proxy assertTrue(AopUtils.isJdkDynamicProxy(bean)); // test serializability assertEquals("bar", bean.foo(1)); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertEquals("bar", deserialized.foo(1)); context.close(); }
@Test public void getWithTransactionManager() throws Exception { try (GenericApplicationContext context = new GenericApplicationContext()) { context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
@Test public void eventListenerWorksWithCustomScope() throws Exception { load(CustomScopeTestBean.class); CustomScope customScope = new CustomScope(); this.context.getBeanFactory().registerScope("custom", customScope); CustomScopeTestBean proxy = this.context.getBean(CustomScopeTestBean.class); assertTrue("bean should be a cglib proxy", AopUtils.isCglibProxy(proxy)); this.eventCollector.assertNoEventReceived(proxy.getId()); this.context.publishEvent(new ContextRefreshedEvent(this.context)); this.eventCollector.assertNoEventReceived(proxy.getId()); customScope.active = false; this.context.publishEvent(new ContextRefreshedEvent(this.context)); customScope.active = true; this.eventCollector.assertNoEventReceived(proxy.getId()); TestEvent event = new TestEvent(); this.context.publishEvent(event); this.eventCollector.assertEvent(proxy.getId(), event); this.eventCollector.assertTotalEventsCount(1); try { customScope.active = false; this.context.publishEvent(new TestEvent()); fail("Should have thrown IllegalStateException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.getCause() instanceof IllegalStateException); } }
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); context.getBeanFactory().registerScope("request", new SimpleMapScope());
public void getFromScope() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());