Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#setSerializationId()
The following examples show how to use
org.springframework.beans.factory.support.DefaultListableBeanFactory#setSerializationId() .
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: AbstractRefreshableApplicationContext.java From spring-analysis-note with MIT License | 7 votes |
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { // 在更新时,如果发现已经存在,将会把之前的 bean 清理掉,并且关闭老 bean 容器 if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); // 注释 1.3 开始加载 (bean 注册) loadBeanDefinitions(beanFactory); // 由于 beanFactory 是公共变量,存在多线程操作,所以加锁操作,避免混乱修改 synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
Example 2
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithTypedMapMethod() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryMapMethodInjectionBean bean = (ObjectFactoryMapMethodInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMapMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 3
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithTypedMapField() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryMapFieldInjectionBean bean = (ObjectFactoryMapFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMapFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 4
Source File: LocalContainerEntityManagerFactoryBeanTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testEntityManagerFactoryIsProxied() throws Exception { LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); assertTrue(emf.equals(emf)); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setSerializationId("emf-bf"); bf.registerSingleton("emf", cefb); cefb.setBeanFactory(bf); cefb.setBeanName("emf"); assertNotNull(SerializationTestUtils.serializeAndDeserialize(emf)); }
Example 5
Source File: AbstractRefreshableApplicationContext.java From java-technology-stack with MIT License | 6 votes |
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
Example 6
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithTypedListMethod() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryListMethodInjectionBean bean = (ObjectFactoryListMethodInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryListMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 7
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithTypedListField() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryListFieldInjectionBean bean = (ObjectFactoryListFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 8
Source File: LocalContainerEntityManagerFactoryBeanTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testEntityManagerFactoryIsProxied() throws Exception { LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); assertTrue(emf.equals(emf)); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setSerializationId("emf-bf"); bf.registerSingleton("emf", cefb); cefb.setBeanFactory(bf); cefb.setBeanName("emf"); assertNotNull(SerializationTestUtils.serializeAndDeserialize(emf)); }
Example 9
Source File: AbstractRefreshableApplicationContext.java From lams with GNU General Public License v2.0 | 6 votes |
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
Example 10
Source File: AbstractRefreshableApplicationContext.java From spring4-understanding with Apache License 2.0 | 6 votes |
/**��ʵ��ִ�д������ĵĵײ�bean factory��ʵ��ˢ�¡� * �ر���ǰ��bean����������еĻ����ͳ�ʼ���������ĵ��������ڵ���һ�ε�����bean���� * <p> * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
Example 11
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithBeanMethod() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryMethodInjectionBean bean = (ObjectFactoryMethodInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 12
Source File: LocalContainerEntityManagerFactoryBeanTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testEntityManagerFactoryIsProxied() throws Exception { LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); assertTrue(emf.equals(emf)); DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.setSerializationId("emf-bf"); bf.registerSingleton("emf", cefb); cefb.setBeanFactory(bf); cefb.setBeanName("emf"); assertNotNull(SerializationTestUtils.serializeAndDeserialize(emf)); }
Example 13
Source File: AutowiredAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactorySerialization() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryInjectionBean bean = (ObjectFactoryInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 14
Source File: InjectAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testObjectFactoryWithBeanField() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); bf.destroySingletons(); }
Example 15
Source File: ScopedProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testJdkScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT); bf.setSerializationId("X"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); ITestBean bean = (ITestBean) bf.getBean("testBean"); assertNotNull(bean); assertTrue(AopUtils.isJdkDynamicProxy(bean)); assertTrue(bean instanceof ScopedObject); ScopedObject scoped = (ScopedObject) bean; assertEquals(TestBean.class, scoped.getTargetObject().getClass()); bean.setAge(101); assertTrue(scope.getMap().containsKey("testBeanTarget")); assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass()); ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertTrue(AopUtils.isJdkDynamicProxy(deserialized)); assertEquals(101, bean.getAge()); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }
Example 16
Source File: ScopedProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testCglibScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT); bf.setSerializationId("Y"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); TestBean tb = (TestBean) bf.getBean("testBean"); assertTrue(AopUtils.isCglibProxy(tb.getFriends())); assertTrue(tb.getFriends() instanceof ScopedObject); ScopedObject scoped = (ScopedObject) tb.getFriends(); assertEquals(ArrayList.class, scoped.getTargetObject().getClass()); tb.getFriends().add("myFriend"); assertTrue(scope.getMap().containsKey("scopedTarget.scopedList")); assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass()); ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends()); assertNotNull(deserialized); assertTrue(AopUtils.isCglibProxy(deserialized)); assertTrue(deserialized.contains("myFriend")); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }
Example 17
Source File: ScopedProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testCglibScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT); bf.setSerializationId("Y"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); TestBean tb = (TestBean) bf.getBean("testBean"); assertTrue(AopUtils.isCglibProxy(tb.getFriends())); assertTrue(tb.getFriends() instanceof ScopedObject); ScopedObject scoped = (ScopedObject) tb.getFriends(); assertEquals(ArrayList.class, scoped.getTargetObject().getClass()); tb.getFriends().add("myFriend"); assertTrue(scope.getMap().containsKey("scopedTarget.scopedList")); assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass()); ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends()); assertNotNull(deserialized); assertTrue(AopUtils.isCglibProxy(deserialized)); assertTrue(deserialized.contains("myFriend")); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }
Example 18
Source File: ScopedProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testJdkScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT); bf.setSerializationId("X"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); ITestBean bean = (ITestBean) bf.getBean("testBean"); assertNotNull(bean); assertTrue(AopUtils.isJdkDynamicProxy(bean)); assertTrue(bean instanceof ScopedObject); ScopedObject scoped = (ScopedObject) bean; assertEquals(TestBean.class, scoped.getTargetObject().getClass()); bean.setAge(101); assertTrue(scope.getMap().containsKey("testBeanTarget")); assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass()); ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertTrue(AopUtils.isJdkDynamicProxy(deserialized)); assertEquals(101, bean.getAge()); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }
Example 19
Source File: ScopedProxyTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testCglibScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(LIST_CONTEXT); bf.setSerializationId("Y"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); TestBean tb = (TestBean) bf.getBean("testBean"); assertTrue(AopUtils.isCglibProxy(tb.getFriends())); assertTrue(tb.getFriends() instanceof ScopedObject); ScopedObject scoped = (ScopedObject) tb.getFriends(); assertEquals(ArrayList.class, scoped.getTargetObject().getClass()); tb.getFriends().add("myFriend"); assertTrue(scope.getMap().containsKey("scopedTarget.scopedList")); assertEquals(ArrayList.class, scope.getMap().get("scopedTarget.scopedList").getClass()); ArrayList<?> deserialized = (ArrayList<?>) SerializationTestUtils.serializeAndDeserialize(tb.getFriends()); assertNotNull(deserialized); assertTrue(AopUtils.isCglibProxy(deserialized)); assertTrue(deserialized.contains("myFriend")); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(ArrayList.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }
Example 20
Source File: ScopedProxyTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testJdkScopedProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(TESTBEAN_CONTEXT); bf.setSerializationId("X"); SimpleMapScope scope = new SimpleMapScope(); bf.registerScope("request", scope); ITestBean bean = (ITestBean) bf.getBean("testBean"); assertNotNull(bean); assertTrue(AopUtils.isJdkDynamicProxy(bean)); assertTrue(bean instanceof ScopedObject); ScopedObject scoped = (ScopedObject) bean; assertEquals(TestBean.class, scoped.getTargetObject().getClass()); bean.setAge(101); assertTrue(scope.getMap().containsKey("testBeanTarget")); assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass()); ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(bean); assertNotNull(deserialized); assertTrue(AopUtils.isJdkDynamicProxy(deserialized)); assertEquals(101, bean.getAge()); assertTrue(deserialized instanceof ScopedObject); ScopedObject scopedDeserialized = (ScopedObject) deserialized; assertEquals(TestBean.class, scopedDeserialized.getTargetObject().getClass()); bf.setSerializationId(null); }