1 16 17 package org.springframework.aop.framework.autoproxy; 18 19 import java.lang.reflect.Proxy ; 20 21 import junit.framework.TestCase; 22 import org.aopalliance.intercept.MethodInterceptor; 23 import org.aopalliance.intercept.MethodInvocation; 24 25 import org.springframework.aop.TargetSource; 26 import org.springframework.aop.support.AopUtils; 27 import org.springframework.beans.ITestBean; 28 import org.springframework.beans.IndexedTestBean; 29 import org.springframework.beans.MutablePropertyValues; 30 import org.springframework.beans.TestBean; 31 import org.springframework.beans.factory.DummyFactory; 32 import org.springframework.beans.factory.FactoryBean; 33 import org.springframework.beans.factory.config.BeanDefinitionHolder; 34 import org.springframework.beans.factory.support.RootBeanDefinition; 35 import org.springframework.context.MessageSource; 36 import org.springframework.context.support.StaticApplicationContext; 37 import org.springframework.context.support.StaticMessageSource; 38 39 43 public class AutoProxyCreatorTests extends TestCase { 44 45 public void testCustomAutoProxyCreator() { 46 StaticApplicationContext sac = new StaticApplicationContext(); 47 sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, null); 48 sac.registerSingleton("singletonNoInterceptor", TestBean.class, null); 49 sac.registerSingleton("singletonToBeProxied", TestBean.class, null); 50 sac.registerPrototype("prototypeToBeProxied", TestBean.class, null); 51 sac.refresh(); 52 53 MessageSource messageSource = (MessageSource) sac.getBean("messageSource"); 54 ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor"); 55 ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied"); 56 ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied"); 57 assertFalse(AopUtils.isCglibProxy(messageSource)); 58 assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor)); 59 assertTrue(AopUtils.isCglibProxy(singletonToBeProxied)); 60 assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied)); 61 62 TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator"); 63 assertEquals(0, tapc.testInterceptor.nrOfInvocations); 64 singletonNoInterceptor.getName(); 65 assertEquals(0, tapc.testInterceptor.nrOfInvocations); 66 singletonToBeProxied.getAge(); 67 assertEquals(1, tapc.testInterceptor.nrOfInvocations); 68 prototypeToBeProxied.getSpouse(); 69 assertEquals(2, tapc.testInterceptor.nrOfInvocations); 70 } 71 72 public void testBeanNameAutoProxyCreator() { 73 StaticApplicationContext sac = new StaticApplicationContext(); 74 sac.registerSingleton("testInterceptor", TestInterceptor.class, null); 75 76 RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class, null); 77 proxyCreator.getPropertyValues().addPropertyValue("interceptorNames", "testInterceptor"); 78 proxyCreator.getPropertyValues().addPropertyValue("beanNames", "singletonToBeProxied,innerBean"); 79 sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator); 80 81 RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, RootBeanDefinition.AUTOWIRE_BY_TYPE); 82 RootBeanDefinition innerBean = new RootBeanDefinition(TestBean.class, null); 83 bd.getPropertyValues().addPropertyValue("spouse", new BeanDefinitionHolder(innerBean, "innerBean")); 84 sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd); 85 86 sac.registerSingleton("autowiredIndexedTestBean", IndexedTestBean.class, new MutablePropertyValues()); 87 88 sac.refresh(); 89 90 MessageSource messageSource = (MessageSource) sac.getBean("messageSource"); 91 ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied"); 92 assertFalse(Proxy.isProxyClass(messageSource.getClass())); 93 assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass())); 94 assertTrue(Proxy.isProxyClass(singletonToBeProxied.getSpouse().getClass())); 95 96 assertEquals(sac.getBean("autowiredIndexedTestBean"), singletonToBeProxied.getNestedIndexedBean()); 98 99 TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor"); 100 assertEquals(2, ti.nrOfInvocations); 102 singletonToBeProxied.getName(); 103 singletonToBeProxied.getSpouse().getName(); 104 assertEquals(5, ti.nrOfInvocations); 105 } 106 107 public void testAutoProxyCreatorWithFactoryBean() { 108 StaticApplicationContext sac = new StaticApplicationContext(); 109 sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, null); 110 111 MutablePropertyValues pvs = new MutablePropertyValues(); 112 pvs.addPropertyValue("singleton", "false"); 113 sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs); 114 115 sac.refresh(); 116 117 FactoryBean prototypeFactory = (FactoryBean) sac.getBean("&prototypeFactoryToBeProxied"); 118 assertTrue(AopUtils.isCglibProxy(prototypeFactory)); 119 120 TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator"); 121 tapc.testInterceptor.nrOfInvocations = 0; 122 sac.getBean("prototypeFactoryToBeProxied"); 123 assertEquals(1, tapc.testInterceptor.nrOfInvocations); 124 } 125 126 127 public static class TestAutoProxyCreator extends AbstractAutoProxyCreator { 128 129 public TestInterceptor testInterceptor = new TestInterceptor(); 130 131 public TestAutoProxyCreator() { 132 setProxyTargetClass(true); 133 setOrder(0); 134 } 135 136 protected Object [] getAdvicesAndAdvisorsForBean(Class beanClass, String name, TargetSource customTargetSource) { 137 if (StaticMessageSource.class.equals(beanClass)) { 138 return DO_NOT_PROXY; 139 } 140 else if (name.endsWith("ToBeProxied")) { 141 return new Object [] {this.testInterceptor}; 142 } 143 else { 144 return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS; 145 } 146 } 147 } 148 149 150 153 public static class TestInterceptor implements MethodInterceptor { 154 155 public int nrOfInvocations = 0; 156 157 public Object invoke(MethodInvocation invocation) throws Throwable { 158 if (!invocation.getMethod().getName().equals("finalize")) { 159 this.nrOfInvocations++; 160 } 161 return invocation.proceed(); 162 } 163 } 164 165 } 166 | Popular Tags |