Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#registerScope()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#registerScope() . 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: ScopingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private GenericApplicationContext createContext(Class<?> configClass) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	if (customScope != null) {
		beanFactory.registerScope(SCOPE, customScope);
	}
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(beanFactory);
	ctx.refresh();
	return ctx;
}
 
Example 2
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 3
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 4
Source File: SimpleScopeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	beanFactory = new DefaultListableBeanFactory();
	Scope scope = new NoOpScope() {
		private int index;
		private List<TestBean> objects = new LinkedList<>(); {
			objects.add(new TestBean());
			objects.add(new TestBean());
		}
		@Override
		public Object get(String name, ObjectFactory<?> objectFactory) {
			if (index >= objects.size()) {
				index = 0;
			}
			return objects.get(index++);
		}
	};

	beanFactory.registerScope("myScope", scope);

	String[] scopeNames = beanFactory.getRegisteredScopeNames();
	assertEquals(1, scopeNames.length);
	assertEquals("myScope", scopeNames[0]);
	assertSame(scope, beanFactory.getRegisteredScope("myScope"));

	new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
			qualifiedResource(SimpleScopeTests.class, "context.xml"));
}
 
Example 5
Source File: ScopingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private GenericApplicationContext createContext(Class<?> configClass) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	if (customScope != null) {
		beanFactory.registerScope(SCOPE, customScope);
	}
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(beanFactory);
	ctx.refresh();
	return ctx;
}
 
Example 6
Source File: ScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 7
Source File: ScopedProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 8
Source File: SimpleScopeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	beanFactory = new DefaultListableBeanFactory();
	Scope scope = new NoOpScope() {
		private int index;
		private List<TestBean> objects = new LinkedList<>(); {
			objects.add(new TestBean());
			objects.add(new TestBean());
		}
		@Override
		public Object get(String name, ObjectFactory<?> objectFactory) {
			if (index >= objects.size()) {
				index = 0;
			}
			return objects.get(index++);
		}
	};

	beanFactory.registerScope("myScope", scope);

	String[] scopeNames = beanFactory.getRegisteredScopeNames();
	assertEquals(1, scopeNames.length);
	assertEquals("myScope", scopeNames[0]);
	assertSame(scope, beanFactory.getRegisteredScope("myScope"));

	XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
	xbdr.loadBeanDefinitions(CONTEXT);
}
 
Example 9
Source File: ScopingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private GenericApplicationContext createContext(Class<?> configClass) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	if (customScope != null) {
		beanFactory.registerScope(SCOPE, customScope);
	}
	beanFactory.registerBeanDefinition("config", new RootBeanDefinition(configClass));
	GenericApplicationContext ctx = new GenericApplicationContext(beanFactory);
	ctx.addBeanFactoryPostProcessor(new ConfigurationClassPostProcessor());
	ctx.refresh();
	return ctx;
}
 
Example 10
Source File: ScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: ScopedProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: SimpleScopeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	beanFactory = new DefaultListableBeanFactory();
	Scope scope = new NoOpScope() {
		private int index;
		private List<TestBean> objects = new LinkedList<TestBean>(); {
			objects.add(new TestBean());
			objects.add(new TestBean());
		}
		@Override
		public Object get(String name, ObjectFactory<?> objectFactory) {
			if (index >= objects.size()) {
				index = 0;
			}
			return objects.get(index++);
		}
	};

	beanFactory.registerScope("myScope", scope);

	String[] scopeNames = beanFactory.getRegisteredScopeNames();
	assertEquals(1, scopeNames.length);
	assertEquals("myScope", scopeNames[0]);
	assertSame(scope, beanFactory.getRegisteredScope("myScope"));

	XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
	xbdr.loadBeanDefinitions(CONTEXT);
}