/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses a default class loader: Usually, the thread context class loader * (if necessary for proxy creation). * @return the proxy object */ public Object getProxy() { return createAopProxy().getProxy(); }
/** * Subclasses should call this to get a new AOP proxy. They should <b>not</b> * create an AOP proxy with {@code this} as an argument. */ protected final synchronized AopProxy createAopProxy() { if (!this.active) { activate(); } return getAopProxyFactory().createAopProxy(this); }
/** * 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); }
/** * Create a proxy for the specified {@code TargetSource}, * implementing the specified interface. * @param proxyInterface the interface that the proxy should implement * @param targetSource the TargetSource that the proxy should invoke * @return the proxy object * @see #ProxyFactory(Class, org.springframework.aop.TargetSource) */ @SuppressWarnings("unchecked") public static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource) { return (T) new ProxyFactory(proxyInterface, targetSource).getProxy(); }
@Test(expected = IllegalStateException.class) // Should fail to get proxy as exposeProxy wasn't set to true public void testTargetCantGetProxyByDefault() { NeedsToSeeProxy et = new NeedsToSeeProxy(); ProxyFactory pf1 = new ProxyFactory(et); assertFalse(pf1.isExposeProxy()); INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(pf1); proxied.incrementViaProxy(); }
/** * 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 new ProxyFactory. * <p>No target, only interfaces. Must add interceptors. * @param proxyInterfaces the interfaces that the proxy should implement */ public ProxyFactory(Class<?>... proxyInterfaces) { setInterfaces(proxyInterfaces); }
@Test public void testWithNoArgConstructor() { NoArgCtorTestBean target = new NoArgCtorTestBean("b", 1); target.reset(); mockTargetSource.setTarget(target); AdvisedSupport pc = new AdvisedSupport(); pc.setTargetSource(mockTargetSource); CglibAopProxy aop = new CglibAopProxy(pc); aop.setConstructorArguments(new Object[] {"Rob Harrop", 22}, new Class<?>[] {String.class, int.class}); NoArgCtorTestBean proxy = (NoArgCtorTestBean) aop.getProxy(); assertNotNull(proxy); }
@Test public void testEqualsAndHashCodeDefined() { AdvisedSupport as = new AdvisedSupport(Named.class); as.setTarget(new Person()); JdkDynamicAopProxy aopProxy = new JdkDynamicAopProxy(as); Named proxy = (Named) aopProxy.getProxy(); Named named = new Person(); assertEquals("equals()", proxy, named); assertEquals("hashCode()", proxy.hashCode(), named.hashCode()); }
@Test(expected = AopConfigException.class) public void testNoInterceptorsAndNoTarget() { AdvisedSupport pc = new AdvisedSupport(ITestBean.class); // Add no interceptors AopProxy aop = createAopProxy(pc); aop.getProxy(); }
@Test public void testProxyNotWrappedIfIncompatible() { FooBar bean = new FooBar(); ProxyCreatorSupport as = new ProxyCreatorSupport(); as.setInterfaces(Foo.class); as.setTarget(bean); Foo proxy = (Foo) createProxy(as); assertSame("Target should be returned when return types are incompatible", bean, proxy.getBarThis()); assertSame("Proxy should be returned when return types are compatible", proxy, proxy.getFooThis()); }
/** * Set the names of the interfaces we're proxying. If no interface * is given, a CGLIB for the actual class will be created. * <p>This is essentially equivalent to the "setInterfaces" method, * but mirrors TransactionProxyFactoryBean's "setProxyInterfaces". * @see #setInterfaces * @see AbstractSingletonProxyFactoryBean#setProxyInterfaces */ public void setProxyInterfaces(Class<?>[] proxyInterfaces) throws ClassNotFoundException { setInterfaces(proxyInterfaces); }
@Override public void incrementViaProxy() { INeedsToSeeProxy thisViaProxy = (INeedsToSeeProxy) AopContext.currentProxy(); thisViaProxy.increment(); Advised advised = (Advised) thisViaProxy; checkAdvised(advised); }
@Override public boolean removeAdvice(Advice advice) throws AopConfigException { int index = indexOf(advice); if (index == -1) { return false; } else { removeAdvisor(index); return true; } }
@Override public void addAdvisor(int pos, Advisor advisor) throws AopConfigException { if (advisor instanceof IntroductionAdvisor) { validateIntroductionAdvisor((IntroductionAdvisor) advisor); } addAdvisorInternal(pos, advisor); }
/** * Create a AdvisedSupport instance with the given parameters. * @param interfaces the proxied interfaces */ public AdvisedSupport(Class<?>... interfaces) { this(); setInterfaces(interfaces); }
/** * Activate this proxy configuration. * @see AdvisedSupportListener#activated */ private void activate() { this.active = true; for (AdvisedSupportListener listener : this.listeners) { listener.activated(this); } }
@Override protected AopProxy createAopProxy(AdvisedSupport as) { as.setProxyTargetClass(true); return new CglibAopProxy(as); }
@Override public void setBeanFactory(BeanFactory beanFactory) { this.beanFactory = beanFactory; checkInterceptorNames(); }
public TestDynamicPointcutAdvice(MethodInterceptor mi, final String pattern) { super(mi); setPointcut(new DynamicMethodMatcherPointcut() { @Override public boolean matches(Method m, @Nullable Class<?> targetClass, Object... args) { boolean run = m.getName().contains(pattern); if (run) ++count; return run; } }); } }