/** * 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 proxy for the specified {@code TargetSource} that extends * the target class of the {@code TargetSource}. * @param targetSource the TargetSource that the proxy should invoke * @return the proxy object */ public static Object getProxy(TargetSource targetSource) { if (targetSource.getTargetClass() == null) { throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class"); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(targetSource); proxyFactory.setProxyTargetClass(true); return proxyFactory.getProxy(); }
pf.setTargetSource(ts); if (element.lookupType.isInterface()) { pf.addInterface(element.lookupType);
pf.setTargetSource(ts); if (element.lookupType.isInterface()) { pf.addInterface(element.lookupType);
/** * Returns a {@link CrudMethodMetadata} proxy that will lookup the actual target object by obtaining a thread bound * instance from the {@link TransactionSynchronizationManager} later. */ public CrudMethodMetadata getCrudMethodMetadata() { ProxyFactory factory = new ProxyFactory(); factory.addInterface(CrudMethodMetadata.class); factory.setTargetSource(new ThreadBoundTargetSource()); return (CrudMethodMetadata) factory.getProxy(this.classLoader); }
pf.setTargetSource(ts); Class<?> dependencyType = descriptor.getDependencyType(); if (dependencyType.isInterface()) {
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableBeanFactory)) { throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory); } ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory; this.scopedTargetSource.setBeanFactory(beanFactory); ProxyFactory pf = new ProxyFactory(); pf.copyFrom(this); pf.setTargetSource(this.scopedTargetSource); Assert.notNull(this.targetBeanName, "Property 'targetBeanName' is required"); Class<?> beanType = beanFactory.getType(this.targetBeanName); if (beanType == null) { throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName + "': Target type could not be determined at the time of proxy creation."); } if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) { pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader())); } // Add an introduction that implements only the methods on ScopedObject. ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName()); pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject)); // Add the AopInfrastructureBean marker to indicate that the scoped proxy // itself is not subject to auto-proxying! Only its target bean is. pf.addInterface(AopInfrastructureBean.class); this.proxy = pf.getProxy(cbf.getBeanClassLoader()); }
pf.setTargetSource(ts); Class<?> dependencyType = descriptor.getDependencyType(); if (dependencyType.isInterface()) {
targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource);
private <T> T createLazyProxy(AtomicReference<T> reference, Class<T> type) { ProxyFactory factory = new ProxyFactory(); factory.setTargetSource(new ReferenceTargetSource<>(reference)); factory.addAdvice(new PassthruAdvice()); factory.setInterfaces(new Class<?>[] { type }); @SuppressWarnings("unchecked") T proxy = (T) factory.getProxy(); return proxy; }
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(this.beanFactory); proxyFactory.setTargetSource(targetSource);
proxyFactory.setTargetSource(targetSource);
proxyFactory.setTargetSource(targetSource); customizeProxyFactory(proxyFactory);
proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate())); proxyFactory.setTargetSource(targetSource); return proxyFactory.getProxy(jof.beanClassLoader);
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate())); proxyFactory.setTargetSource(targetSource); return proxyFactory.getProxy(jof.beanClassLoader);
@Test public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(); pc.addInterface(ITestBean.class); // Could apply dynamically to getAge/setAge but not to getName TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age"); pc.addAdvisor(dp); this.mockTargetSource.setTarget(tb); pc.setTargetSource(mockTargetSource); ITestBean it = (ITestBean) createProxy(pc); assertEquals(0, dp.count); it.getAge(); // Statically vetoed assertEquals(0, dp.count); it.setAge(11); assertEquals(11, it.getAge()); assertEquals(1, dp.count); // Applies statically but not dynamically it.setName("joe"); assertEquals(1, dp.count); }
@Test public void testSerialization() throws Exception { SerializablePerson sp1 = new SerializablePerson(); sp1.setName("Tony"); SerializablePerson sp2 = new SerializablePerson(); sp1.setName("Gordon"); HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1); ProxyFactory pf = new ProxyFactory(); pf.addInterface(Person.class); pf.setTargetSource(hts); pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor())); Person p = (Person) pf.getProxy(); assertEquals(sp1.getName(), p.getName()); hts.swap(sp2); assertEquals(sp2.getName(), p.getName()); p = (Person) SerializationTestUtils.serializeAndDeserialize(p); // We need to get a reference to the client-side targetsource hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource(); assertEquals(sp2.getName(), p.getName()); hts.swap(sp1); assertEquals(sp1.getName(), p.getName()); } }
pc.setTargetSource(hts); assertEquals(tb3.getAge(), proxy.getAge()); assertEquals(nop.getCount(), 3);