/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
@Test @SuppressWarnings("resource") public void testName() throws Exception { scope.setName("foo"); StaticApplicationContext beanFactory = new StaticApplicationContext(); scope.postProcessBeanFactory(beanFactory.getDefaultListableBeanFactory()); String[] scopes = beanFactory.getDefaultListableBeanFactory().getRegisteredScopeNames(); assertEquals(1, scopes.length); assertEquals("foo", scopes[0]); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); m.put("name", "Roderick"); parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); StaticMessageSource messageSource = sac.getStaticMessageSource(); Map<String, String> usMessages = new HashMap<>(3); usMessages.put("message.format.example1", MSG_TXT1_US); usMessages.put("message.format.example2", MSG_TXT2_US); usMessages.put("message.format.example3", MSG_TXT3_US); messageSource.addMessages(usMessages, Locale.US); messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK); return sac; }
@SuppressWarnings("resource") @Test public void testName() throws Exception { scope.setName("foo"); StaticApplicationContext beanFactory = new StaticApplicationContext(); scope.postProcessBeanFactory(beanFactory.getDefaultListableBeanFactory()); String[] scopes = beanFactory.getDefaultListableBeanFactory().getRegisteredScopeNames(); assertEquals(1, scopes.length); assertEquals("foo", scopes[0]); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
context.registerBeanDefinition("listener2", listener2); context.refresh(); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event2 = new MyEvent(context); context.publishEvent(event2); assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event3 = new MyEvent(context); context.publishEvent(event3);
@Test public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() { StaticApplicationContext sac = new StaticApplicationContext(); sac.registerSingleton("testInterceptor", TestInterceptor.class); RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class); proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor"); proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied"); sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd); sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class); sac.refresh(); ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied"); assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass())); TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor"); int initialNr = ti.nrOfInvocations; singletonToBeProxied.getName(); assertEquals(initialNr + 1, ti.nrOfInvocations); FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied"); assertTrue(Proxy.isProxyClass(factory.getClass())); TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied"); assertFalse(AopUtils.isAopProxy(tb)); assertEquals(initialNr + 3, ti.nrOfInvocations); tb.getAge(); assertEquals(initialNr + 3, ti.nrOfInvocations); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); Resource resource = new ClassPathResource("testBeans.properties", getClass()); reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1")); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor"); proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,innerBean,singletonFactoryToBeProxied"); sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator); RootBeanDefinition innerBean = new RootBeanDefinition(TestBean.class); bd.getPropertyValues().add("spouse", new BeanDefinitionHolder(innerBean, "innerBean")); sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);
@Test public void testQualifiedByAttributesWithCustomQualifierRegistered() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver) context.getDefaultListableBeanFactory().getAutowireCandidateResolver(); resolver.addQualifierType(MultipleAttributeQualifier.class); context.registerSingleton("testBean", MultiQualifierClient.class); context.refresh(); MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean"); assertNotNull( testBean.factoryTheta); assertNotNull( testBean.implTheta); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */ public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }