Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setLazyInit()
The following examples show how to use
org.springframework.beans.factory.support.RootBeanDefinition#setLazyInit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
private void doTestLazyResourceInjection(Class<? extends TestBeanHolder> annotatedBeanClass) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); RootBeanDefinition abd = new RootBeanDefinition(annotatedBeanClass); abd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); ac.registerBeanDefinition("annotatedBean", abd); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); tbd.setLazyInit(true); ac.registerBeanDefinition("testBean", tbd); ac.refresh(); TestBeanHolder bean = ac.getBean("annotatedBean", TestBeanHolder.class); assertFalse(ac.getBeanFactory().containsSingleton("testBean")); assertNotNull(bean.getTestBean()); assertNull(bean.getTestBean().getName()); assertTrue(ac.getBeanFactory().containsSingleton("testBean")); TestBean tb = (TestBean) ac.getBean("testBean"); tb.setName("tb"); assertSame("tb", bean.getTestBean().getName()); }
Example 2
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testLazyResourceInjectionWithField() { doTestLazyResourceInjection(FieldResourceInjectionBean.class); AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); RootBeanDefinition abd = new RootBeanDefinition(FieldResourceInjectionBean.class); abd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); ac.registerBeanDefinition("annotatedBean", abd); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); tbd.setLazyInit(true); ac.registerBeanDefinition("testBean", tbd); ac.refresh(); FieldResourceInjectionBean bean = ac.getBean("annotatedBean", FieldResourceInjectionBean.class); assertFalse(ac.getBeanFactory().containsSingleton("testBean")); assertFalse(bean.getTestBeans().isEmpty()); assertNull(bean.getTestBeans().get(0).getName()); assertTrue(ac.getBeanFactory().containsSingleton("testBean")); TestBean tb = (TestBean) ac.getBean("testBean"); tb.setName("tb"); assertSame("tb", bean.getTestBean().getName()); }
Example 3
Source File: BaymaxBeanDefinitionParser.java From baymax with Apache License 2.0 | 6 votes |
/** * ID处理 * 如果标签上没有定义name 则自动生成name * @param element * @param parserContext */ private Pair<RootBeanDefinition, String/*name*/> fixName(Element element, ParserContext parserContext, Class beanClass){ RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(beanClass); beanDefinition.setLazyInit(false); String name = element.getAttribute("name"); synchronized (BaymaxBeanDefinitionParser.class){ if (name == null || name.length() == 0) { name = "Baymax-" + beanClass.getSimpleName(); while (parserContext.getRegistry().containsBeanDefinition(name)) { name += (counter++); } } if (name != null && name.length() > 0) { if (parserContext.getRegistry().containsBeanDefinition(name)) { throw new IllegalStateException("Duplicate spring bean id " + name); } parserContext.getRegistry().registerBeanDefinition(name, beanDefinition); } } return new Pair<RootBeanDefinition, String>(beanDefinition, name); }
Example 4
Source File: AutowiredAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testObjectProviderInjectionWithUnresolvedOrderedStream() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class); tb1.setFactoryMethodName("newTestBean1"); tb1.setPrimary(true); bf.registerBeanDefinition("testBean1", tb1); RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class); tb2.setFactoryMethodName("newTestBean2"); tb2.setLazyInit(true); bf.registerBeanDefinition("testBean2", tb2); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); List<?> testBeans = bean.sortedTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean2"), testBeans.get(0)); assertSame(bf.getBean("testBean1"), testBeans.get(1)); }
Example 5
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testLazyInitFlag() { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setLazyInit(true); factory.registerBeanDefinition("tb1", bd1); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setLazyInit(false); factory.registerBeanDefinition("tb2", bd2); factory.registerBeanDefinition("tb3", new RootBeanDefinition(TestBean.class)); assertEquals(Boolean.TRUE, ((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb1")).getLazyInit()); assertEquals(Boolean.FALSE, ((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb2")).getLazyInit()); assertNull(((AbstractBeanDefinition) factory.getMergedBeanDefinition("tb3")).getLazyInit()); factory.preInstantiateSingletons(); assertFalse(factory.containsSingleton("tb1")); assertTrue(factory.containsSingleton("tb2")); assertTrue(factory.containsSingleton("tb3")); }
Example 6
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 6 votes |
private void doTestLazyResourceInjection(Class<? extends TestBeanHolder> annotatedBeanClass) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(); RootBeanDefinition abd = new RootBeanDefinition(annotatedBeanClass); abd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); ac.registerBeanDefinition("annotatedBean", abd); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); tbd.setLazyInit(true); ac.registerBeanDefinition("testBean", tbd); ac.refresh(); TestBeanHolder bean = ac.getBean("annotatedBean", TestBeanHolder.class); assertFalse(ac.getBeanFactory().containsSingleton("testBean")); assertNotNull(bean.getTestBean()); assertNull(bean.getTestBean().getName()); assertTrue(ac.getBeanFactory().containsSingleton("testBean")); TestBean tb = (TestBean) ac.getBean("testBean"); tb.setName("tb"); assertSame("tb", bean.getTestBean().getName()); }
Example 7
Source File: MvcNamespaceUtils.java From java-technology-stack with MIT License | 5 votes |
/** * Registers an {@link HandlerMappingIntrospector} under a well-known name * unless already registered. */ private static void registerHandlerMappingIntrospector(ParserContext parserContext, @Nullable Object source) { if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)){ RootBeanDefinition beanDef = new RootBeanDefinition(HandlerMappingIntrospector.class); beanDef.setSource(source); beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); beanDef.setLazyInit(true); parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, beanDef); parserContext.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)); } }
Example 8
Source File: DefaultLifecycleProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleFactoryBean.class); bd.setLazyInit(true); context.registerBeanDefinition("bean", bd); context.refresh(); DummySmartLifecycleFactoryBean bean = context.getBean("&bean", DummySmartLifecycleFactoryBean.class); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); }
Example 9
Source File: InitializingFacade.java From dubbo-plus with Apache License 2.0 | 5 votes |
private void registerSpringBean(Class<?> type){ RootBeanDefinition definition = new RootBeanDefinition(); definition.setBeanClass(ClientFactory.class); definition.setScope(BeanDefinition.SCOPE_SINGLETON); definition.setLazyInit(false); definition.getPropertyValues().addPropertyValue("type",type); beanFactory.registerBeanDefinition(type.getName(),definition); }
Example 10
Source File: DefaultLifecycleProcessorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleFactoryBean.class); bd.setLazyInit(true); context.registerBeanDefinition("bean", bd); context.refresh(); DummySmartLifecycleFactoryBean bean = context.getBean("&bean", DummySmartLifecycleFactoryBean.class); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); }
Example 11
Source File: AutowiredAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testObjectProviderInjectionWithTargetPrimary() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class); tb1.setFactoryMethodName("newTestBean1"); tb1.setPrimary(true); bf.registerBeanDefinition("testBean1", tb1); RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class); tb2.setFactoryMethodName("newTestBean2"); tb2.setLazyInit(true); bf.registerBeanDefinition("testBean2", tb2); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean1"), bean.getTestBean()); assertSame(bf.getBean("testBean1"), bean.getOptionalTestBean()); assertSame(bf.getBean("testBean1"), bean.consumeOptionalTestBean()); assertSame(bf.getBean("testBean1"), bean.getUniqueTestBean()); assertSame(bf.getBean("testBean1"), bean.consumeUniqueTestBean()); assertFalse(bf.containsSingleton("testBean2")); List<?> testBeans = bean.iterateTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.forEachTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.streamTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.sortedTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean2"), testBeans.get(0)); assertSame(bf.getBean("testBean1"), testBeans.get(1)); }
Example 12
Source File: BeanMethodQualificationTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testCustomWithAsm() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName())); RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName()); customPojo.setLazyInit(true); ctx.registerBeanDefinition("customPojo", customPojo); ctx.refresh(); assertFalse(ctx.getBeanFactory().containsSingleton("testBean1")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); CustomPojo pojo = ctx.getBean(CustomPojo.class); assertThat(pojo.testBean.getName(), equalTo("interesting")); }
Example 13
Source File: MvcNamespaceUtils.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Registers an {@link HandlerMappingIntrospector} under a well-known name * unless already registered. */ private static void registerHandlerMappingIntrospector(ParserContext parserContext, Object source) { if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)){ RootBeanDefinition beanDef = new RootBeanDefinition(HandlerMappingIntrospector.class); beanDef.setSource(source); beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); beanDef.setLazyInit(true); parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, beanDef); parserContext.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)); } }
Example 14
Source File: AnnotationConfigApplicationContextTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void individualBeanWithFactoryBeanSupplierAndTargetType() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(); bd.setInstanceSupplier(TypedFactoryBean::new); bd.setTargetType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)); bd.setLazyInit(true); context.registerBeanDefinition("fb", bd); context.refresh(); assertEquals(String.class, context.getType("fb")); assertEquals(FactoryBean.class, context.getType("&fb")); }
Example 15
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithPrimary() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setLazyInit(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPrimary(true); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd2")); assertFalse(lbf.containsSingleton("bd1")); }
Example 16
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testObjectProviderInjectionWithTargetPrimary() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class); tb1.setFactoryMethodName("newTestBean1"); tb1.setPrimary(true); bf.registerBeanDefinition("testBean1", tb1); RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class); tb2.setFactoryMethodName("newTestBean2"); tb2.setLazyInit(true); bf.registerBeanDefinition("testBean2", tb2); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean1"), bean.getTestBean()); assertSame(bf.getBean("testBean1"), bean.getOptionalTestBean()); assertSame(bf.getBean("testBean1"), bean.consumeOptionalTestBean()); assertSame(bf.getBean("testBean1"), bean.getUniqueTestBean()); assertSame(bf.getBean("testBean1"), bean.consumeUniqueTestBean()); assertFalse(bf.containsSingleton("testBean2")); List<?> testBeans = bean.iterateTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.forEachTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.streamTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean1"), testBeans.get(0)); assertSame(bf.getBean("testBean2"), testBeans.get(1)); testBeans = bean.sortedTestBeans(); assertEquals(2, testBeans.size()); assertSame(bf.getBean("testBean2"), testBeans.get(0)); assertSame(bf.getBean("testBean1"), testBeans.get(1)); }
Example 17
Source File: VertxWebTestClientRegistrar.java From vertx-spring-boot with Apache License 2.0 | 5 votes |
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { if (!isWebTestClientRegistered()) { VertxWebTestClientSupplier supplier = new VertxWebTestClientSupplier(applicationContext); RootBeanDefinition definition = new RootBeanDefinition(WebTestClient.class, supplier); definition.setLazyInit(true); registry.registerBeanDefinition(WebTestClient.class.getName(), definition); } }
Example 18
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testGetBeanByTypeWithPrimary() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class); bd1.setLazyInit(true); RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class); bd2.setPrimary(true); lbf.registerBeanDefinition("bd1", bd1); lbf.registerBeanDefinition("bd2", bd2); TestBean bean = lbf.getBean(TestBean.class); assertThat(bean.getBeanName(), equalTo("bd2")); assertFalse(lbf.containsSingleton("bd1")); }
Example 19
Source File: DefaultLifecycleProcessorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleFactoryBean.class); bd.setLazyInit(true); context.registerBeanDefinition("bean", bd); context.refresh(); DummySmartLifecycleFactoryBean bean = context.getBean("&bean", DummySmartLifecycleFactoryBean.class); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); }
Example 20
Source File: BeanMethodQualificationTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testCustomWithAsm() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.registerBeanDefinition("customConfig", new RootBeanDefinition(CustomConfig.class.getName())); RootBeanDefinition customPojo = new RootBeanDefinition(CustomPojo.class.getName()); customPojo.setLazyInit(true); ctx.registerBeanDefinition("customPojo", customPojo); ctx.refresh(); assertFalse(ctx.getBeanFactory().containsSingleton("testBean1")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); CustomPojo pojo = ctx.getBean(CustomPojo.class); assertThat(pojo.testBean.getName(), equalTo("interesting")); }