下面列出了org.springframework.context.support.StaticApplicationContext#registerPrototype ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}