Java Code Examples for org.springframework.context.support.StaticApplicationContext#registerPrototype()

The following examples show how to use org.springframework.context.support.StaticApplicationContext#registerPrototype() . 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: MethodMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private TestMethodMessageHandler initMethodMessageHandler(
		Consumer<TestMethodMessageHandler> customizer, Class<?>... handlerTypes) {

	StaticApplicationContext context = new StaticApplicationContext();
	for (Class<?> handlerType : handlerTypes) {
		String beanName = ClassUtils.getShortNameAsProperty(handlerType);
		context.registerPrototype(beanName, handlerType);
	}
	TestMethodMessageHandler messageHandler = new TestMethodMessageHandler();
	messageHandler.setApplicationContext(context);
	customizer.accept(messageHandler);
	messageHandler.afterPropertiesSet();
	return messageHandler;
}
 
Example 2
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 3
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 4
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
	sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
	sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);

	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 5
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 6
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 7
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
	sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
	sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);

	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 8
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 9
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 10
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
	sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
	sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);

	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}