Search in sources :

Example 1 with DelegatingIntroductionInterceptor

use of org.springframework.aop.support.DelegatingIntroductionInterceptor in project spring-framework by spring-projects.

the class ScopedProxyFactoryBean method setBeanFactory.

@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);
    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());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 2 with DelegatingIntroductionInterceptor

use of org.springframework.aop.support.DelegatingIntroductionInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testAdviceImplementsIntroductionInfo.

@Test
public void testAdviceImplementsIntroductionInfo() throws Throwable {
    TestBean tb = new TestBean();
    String name = "tony";
    tb.setName(name);
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(di);
    final long ts = 37;
    pc.addAdvice(new DelegatingIntroductionInterceptor((TimeStamped) () -> ts));
    ITestBean proxied = (ITestBean) createProxy(pc);
    assertThat(proxied.getName()).isEqualTo(name);
    TimeStamped intro = (TimeStamped) proxied;
    assertThat(intro.getTimeStamp()).isEqualTo(ts);
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) TimeStamped(org.springframework.core.testfixture.TimeStamped) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 3 with DelegatingIntroductionInterceptor

use of org.springframework.aop.support.DelegatingIntroductionInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testIntroductionThrowsUncheckedException.

/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    @SuppressWarnings("serial")
    class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {

        /**
         * @see org.springframework.core.testfixture.TimeStamped#getTimeStamp()
         */
        @Override
        public long getTimeStamp() {
            throw new UnsupportedOperationException();
        }
    }
    pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
    TimeStamped ts = (TimeStamped) createProxy(pc);
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(ts::getTimeStamp);
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) TimeStamped(org.springframework.core.testfixture.TimeStamped) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Pointcuts(org.springframework.aop.support.Pointcuts) Test(org.junit.jupiter.api.Test)

Example 4 with DelegatingIntroductionInterceptor

use of org.springframework.aop.support.DelegatingIntroductionInterceptor in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method createRefreshableProxy.

/**
 * 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) {
        // force use of Class.getClassLoader()
        classLoader = null;
        proxyFactory.setProxyTargetClass(true);
    }
    DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
    introduction.suppressInterface(TargetSource.class);
    proxyFactory.addAdvice(introduction);
    return proxyFactory.getProxy(classLoader);
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 5 with DelegatingIntroductionInterceptor

use of org.springframework.aop.support.DelegatingIntroductionInterceptor in project spring-framework by spring-projects.

the class GenericMessageEndpointFactory method createEndpoint.

/**
 * Wrap each concrete endpoint instance with an AOP proxy,
 * exposing the message listener's interfaces as well as the
 * endpoint SPI through an AOP introduction.
 */
@Override
public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException {
    GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource);
    ProxyFactory proxyFactory = new ProxyFactory(getMessageListener());
    DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint);
    introduction.suppressInterface(MethodInterceptor.class);
    proxyFactory.addAdvice(introduction);
    return (MessageEndpoint) proxyFactory.getProxy();
}
Also used : DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MessageEndpoint(jakarta.resource.spi.endpoint.MessageEndpoint) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Aggregations

DelegatingIntroductionInterceptor (org.springframework.aop.support.DelegatingIntroductionInterceptor)5 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 Test (org.junit.jupiter.api.Test)2 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 TimeStamped (org.springframework.core.testfixture.TimeStamped)2 MessageEndpoint (jakarta.resource.spi.endpoint.MessageEndpoint)1 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)1 Pointcuts (org.springframework.aop.support.Pointcuts)1 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)1 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1