/** * As of 4.2, this method adds {@link TransactionalProxy} to the set of * proxy interfaces in order to avoid re-processing of transaction metadata. */ @Override protected void postProcessProxyFactory(ProxyFactory proxyFactory) { proxyFactory.addInterface(TransactionalProxy.class); }
/** * Create a ProxyFactory for the specified {@code TargetSource}, * making the proxy implement the specified interface. * @param proxyInterface the interface that the proxy should implement * @param targetSource the TargetSource that the proxy should invoke */ public ProxyFactory(Class<?> proxyInterface, TargetSource targetSource) { addInterface(proxyInterface); setTargetSource(targetSource); }
/** * Create a new ProxyFactory for the given interface and interceptor. * <p>Convenience method for creating a proxy for a single interceptor, * assuming that the interceptor handles all calls itself rather than * delegating to a target, like in the case of remoting proxies. * @param proxyInterface the interface that the proxy should implement * @param interceptor the interceptor that the proxy should invoke */ public ProxyFactory(Class<?> proxyInterface, Interceptor interceptor) { addInterface(proxyInterface); addAdvice(interceptor); }
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); Class<?> ifc = getServiceInterface(); Assert.notNull(ifc, "Property 'serviceInterface' is required"); // Build a proxy that also exposes the JAX-WS BindingProvider interface. ProxyFactory pf = new ProxyFactory(); pf.addInterface(ifc); pf.addInterface(BindingProvider.class); pf.addAdvice(this); this.serviceProxy = pf.getProxy(getBeanClassLoader()); }
@Test public void testAopJdkProxyWithMultipleInterfaces() throws Exception { ModelMap map = new ModelMap(); Map<?, ?> target = new HashMap<>(); ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.addInterface(Serializable.class); factory.addInterface(Cloneable.class); factory.addInterface(Comparable.class); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void testAddRepeatedInterface() { TimeStamped tst = new TimeStamped() { @Override public long getTimeStamp() { throw new UnsupportedOperationException("getTimeStamp"); } }; ProxyFactory pf = new ProxyFactory(tst); // We've already implicitly added this interface. // This call should be ignored without error pf.addInterface(TimeStamped.class); // All cool assertThat(pf.getProxy(), instanceOf(TimeStamped.class)); }
private Foo jdkProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.addInterface(Foo.class); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy)); assertThat(proxy, instanceOf(Foo.class)); return proxy; }
@Test public void testProxiedUserInterfacesWithMultipleInterfaces() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); pf.addInterface(Comparable.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(2, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); assertEquals(Comparable.class, userInterfaces[1]); }
protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) { MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator(); mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1)); ProxyFactory pf = new ProxyFactory(target); pf.addInterface(RepositoryInterface.class); addPersistenceExceptionTranslation(pf, mpet); return (RepositoryInterface) pf.getProxy(); }
@Test public void setFieldAndGetFieldViaJdkDynamicProxy() throws Exception { ProxyFactory pf = new ProxyFactory(this.person); pf.addInterface(Person.class); Person proxy = (Person) pf.getProxy(); assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy)); assertSetFieldAndGetFieldBehaviorForProxy(proxy, this.person); }
@Test public void testAopJdkProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); Map<?, ?> target = new HashMap<>(); factory.setTarget(target); factory.addInterface(Map.class); Object proxy = factory.getProxy(); map.addAttribute(proxy); assertSame(proxy, map.get("map")); }
@Test public void invokeListenerInvalidProxy() { Object target = new InvalidProxyTestBean(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(target); proxyFactory.addInterface(SimpleService.class); Object bean = proxyFactory.getProxy(getClass().getClassLoader()); Method method = ReflectionUtils.findMethod( InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean); this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage("handleIt2"); listener.onApplicationEvent(createGenericTestEvent("test")); }
@Test public void testProxiedUserInterfacesWithSingleInterface() { ProxyFactory pf = new ProxyFactory(); pf.setTarget(new TestBean()); pf.addInterface(ITestBean.class); Object proxy = pf.getProxy(); Class<?>[] userInterfaces = AopProxyUtils.proxiedUserInterfaces(proxy); assertEquals(1, userInterfaces.length); assertEquals(ITestBean.class, userInterfaces[0]); }
@Test public void crossClassInterfaceOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new OtherServiceImpl()); proxyFactory.addInterface(OtherService.class); proxyFactory.addAdvice(this.ti); OtherService otherService = (OtherService) proxyFactory.getProxy(); otherService.foo(); assertGetTransactionAndCommitCount(1); }
@Test public void testMixinWithIntroductionInfo() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); // We don't use an IntroductionAdvisor, we can just add an advice that implements IntroductionInfo pc.addAdvice(new LockMixin()); pc.setTarget(tb); testTestBeanIntroduction(pc); }
/** * Test stateful interceptor */ @Test public void testMixinWithIntroductionAdvisor() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); pc.addAdvisor(new LockMixinAdvisor()); pc.setTarget(tb); testTestBeanIntroduction(pc); }
@Test public void crossClassInterfaceMethodLevelOnJdkProxy() { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(new SomeServiceImpl()); proxyFactory.addInterface(SomeService.class); proxyFactory.addAdvice(this.ti); SomeService someService = (SomeService) proxyFactory.getProxy(); someService.bar(); assertGetTransactionAndCommitCount(1); someService.foo(); assertGetTransactionAndCommitCount(2); someService.fooBar(); assertGetTransactionAndCommitCount(3); }
@Test public void testReplaceArgument() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); pc.setTarget(tb); pc.addAdvisor(new StringSetterNullReplacementAdvice()); ITestBean t = (ITestBean) pc.getProxy(); int newAge = 5; t.setAge(newAge); assertEquals(newAge, t.getAge()); String newName = "greg"; t.setName(newName); assertEquals(newName, t.getName()); t.setName(null); // Null replacement magic should work assertEquals("", t.getName()); }
@Test public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge"); pc.addAdvisor(dp); pc.setTarget(tb); ITestBean it = (ITestBean) createProxy(pc); assertEquals(0, dp.count); it.getAge(); assertEquals(1, dp.count); it.setAge(11); assertEquals(11, it.getAge()); assertEquals(2, dp.count); }
@Test public void testStaticMethodPointcut() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); NopInterceptor di = new NopInterceptor(); TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge"); pc.addAdvisor(sp); pc.setTarget(tb); ITestBean it = (ITestBean) createProxy(pc); assertEquals(di.getCount(), 0); it.getAge(); assertEquals(di.getCount(), 1); it.setAge(11); assertEquals(it.getAge(), 11); assertEquals(di.getCount(), 2); }