@Test public void testFactoryBeanAndApplicationListener() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD); ctx.getBeanFactory().registerSingleton("manualFBAAL", new FactoryBeanAndApplicationListener()); assertEquals(2, ctx.getBeansOfType(ApplicationListener.class).size()); ctx.close(); }
@Test public void testInlineScriptFromTag() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass()); BeanDefinition bd = ctx.getBeanFactory().getBeanDefinition("calculator"); assertTrue(ObjectUtils.containsElement(bd.getDependsOn(), "messenger")); Calculator calculator = (Calculator) ctx.getBean("calculator"); assertNotNull(calculator); assertFalse(calculator instanceof Refreshable); }
@Test public void testCustomScopeMetadataResolver() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/customScopeResolverTests.xml"); BeanDefinition bd = context.getBeanFactory().getBeanDefinition("fooServiceImpl"); assertEquals("myCustomScope", bd.getScope()); assertFalse(bd.isSingleton()); }
@Test public void invokeOnLazyInitBean() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/lazyInit.xml"); assertFalse(ctx.getBeanFactory().containsSingleton("testBean")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean2"); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "foo", name); } finally { ctx.close(); } }
@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 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 testUserSpecifiedTaskExecutor() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/user-specified-split-task-executor-context.xml"); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory(); PropertyValue propertyValue = new JsrSplitParser(null).getSplitTaskExecutorPropertyValue(registry); RuntimeBeanReference runtimeBeanReferenceValue = (RuntimeBeanReference) propertyValue.getValue(); Assert.assertTrue("RuntimeBeanReference should have a name of jsr352splitTaskExecutor" , "jsr352splitTaskExecutor".equals(runtimeBeanReferenceValue.getBeanName())); context.close(); }
@Test public void testDefaultTaskExecutor() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/default-split-task-executor-context.xml"); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory(); PropertyValue propertyValue = new JsrSplitParser(null).getSplitTaskExecutorPropertyValue(registry); Assert.assertTrue("Task executor not an instance of SimpleAsyncTaskExecutor" , (propertyValue.getValue() instanceof SimpleAsyncTaskExecutor)); context.close(); }
@Test public void testVanillaConfiguration() throws Exception { assertTrue(context.getBeanFactory().containsBeanDefinition("vanilla")); context.getBean("vanilla"); }
/** * @return the current spring bean factory OR null if there is not one */ public ListableBeanFactory getBeanFactory() { if (applicationContext != null) { return applicationContext.getBeanFactory(); } return null; }
/** * @return the current spring bean factory OR null if there is not one */ public ListableBeanFactory getBeanFactory() { if (applicationContext != null) { return applicationContext.getBeanFactory(); } return null; }
@Test public void testExpressionBasedConfiguration() throws Exception { assertTrue(context.getBeanFactory().containsBeanDefinition("expression")); Object target = context.getBean("expression"); assertNotNull(ReflectionTestUtils.getField(ReflectionTestUtils.getField(target, "handler"), "routingKeyGenerator")); }
public List<String> getServicesNames() { ArrayList<String> beanNames = new ArrayList<String>(); String[] singletons = applicationContext.getBeanFactory().getSingletonNames(); for (String singleton : singletons) { if (singleton.startsWith("org.springframework.context")) { continue; // skip the spring standard ones } beanNames.add(singleton); } Collections.sort(beanNames); return beanNames; }
@Override public List<String> getServicesNames() { ArrayList<String> beanNames = new ArrayList<String>(); String[] singletons = applicationContext.getBeanFactory().getSingletonNames(); for (String singleton : singletons) { if (singleton.startsWith("org.springframework.context")) { continue; // skip the spring standard ones } beanNames.add(singleton); } Collections.sort(beanNames); return beanNames; }
@Override public <T> Map<String, T> getObjectsByType(Class<T> type) { if (isSpringInternalType(type)) { return emptyMap(); } Map<String, T> beans = applicationContext.getBeansOfType(type); return beans.entrySet().stream().filter(entry -> applicationContext.getBeanFactory().containsBeanDefinition(entry.getKey())) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); }
public void registerService(String name, Object service) { if (name == null || service == null) { throw new IllegalArgumentException("name and service must not be null for service registration"); } try { applicationContext.getBeanFactory().autowireBean(service); } catch (BeansException e) { throw new IllegalArgumentException("Invalid service ("+service+") with name ("+name+") registration: " + e.getMessage(), e); } registerBean(name, service); }
@Override public void registerService(String name, Object service) { if (name == null || service == null) { throw new IllegalArgumentException("name and service must not be null for service registration"); } try { applicationContext.getBeanFactory().autowireBean(service); } catch (BeansException e) { throw new IllegalArgumentException( "Invalid service (" + service + ") with name (" + name + ") registration: " + e.getMessage(), e); } registerBean(name, service); }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Test public void must_use_no_context_path_when_ServletContext_is_not_available() throws Exception { ((DefaultListableBeanFactory)context.getBeanFactory()).removeBeanDefinition("servletContext"); underTest = context.getBean(DefaultSiteProvider.class); assertThat(underTest.getContextPath(), is("/")); }