Search in sources :

Example 11 with ProxyFactory

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

the class ControlFlowPointcutTests method testSelectiveApplication.

/**
	 * Check that we can use a cflow pointcut only in conjunction with
	 * a static pointcut: e.g. all setter methods that are invoked under
	 * a particular class. This greatly reduces the number of calls
	 * to the cflow pointcut, meaning that it's not so prohibitively
	 * expensive.
	 */
@Test
public void testSelectiveApplication() {
    TestBean target = new TestBean();
    target.setAge(27);
    NopInterceptor nop = new NopInterceptor();
    ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
    Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
    ProxyFactory pf = new ProxyFactory(target);
    ITestBean proxied = (ITestBean) pf.getProxy();
    pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
    // Not advised, not under One
    target.setAge(16);
    assertEquals(0, nop.getCount());
    // Not advised; under One but not a setter
    assertEquals(16, new One().getAge(proxied));
    assertEquals(0, nop.getCount());
    // Won't be advised
    new One().set(proxied);
    assertEquals(1, nop.getCount());
    // We saved most evaluations
    assertEquals(1, cflow.getEvaluations());
}
Also used : Pointcut(org.springframework.aop.Pointcut) ITestBean(org.springframework.tests.sample.beans.ITestBean) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 12 with ProxyFactory

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

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.

@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
    final long t = 1001L;
    class Tester implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertTrue(ts.getTimeStamp() == t);
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 13 with ProxyFactory

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithSuperInterface.

@Test
public void testIntroductionInterceptorWithSuperInterface() throws Exception {
    TestBean raw = new TestBean();
    assertTrue(!(raw instanceof TimeStamped));
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = mock(SubTimeStamped.class);
    long timestamp = 111L;
    given(ts.getTimeStamp()).willReturn(timestamp);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
    TimeStamped tsp = (TimeStamped) factory.getProxy();
    assertTrue(!(tsp instanceof SubTimeStamped));
    assertTrue(tsp.getTimeStamp() == timestamp);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 14 with ProxyFactory

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

the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithInterfaceHierarchy.

@Test
public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
    TestBean raw = new TestBean();
    assertTrue(!(raw instanceof SubTimeStamped));
    ProxyFactory factory = new ProxyFactory(raw);
    TimeStamped ts = mock(SubTimeStamped.class);
    long timestamp = 111L;
    given(ts.getTimeStamp()).willReturn(timestamp);
    factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
    SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
    assertTrue(tsp.getTimeStamp() == timestamp);
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 15 with ProxyFactory

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

the class MBeanExporter method registerLazyInit.

/**
	 * Registers beans that are configured for lazy initialization with the
	 * {@code MBeanServer} indirectly through a proxy.
	 * @param beanName the name of the bean in the {@code BeanFactory}
	 * @param beanKey the key associated with this bean in the beans map
	 * @return the {@code ObjectName} under which the bean was registered
	 * with the {@code MBeanServer}
	 */
private ObjectName registerLazyInit(String beanName, String beanKey) throws JMException {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setProxyTargetClass(true);
    proxyFactory.setFrozen(true);
    if (isMBean(this.beanFactory.getType(beanName))) {
        // A straight MBean... Let's create a simple lazy-init CGLIB proxy for it.
        LazyInitTargetSource targetSource = new LazyInitTargetSource();
        targetSource.setTargetBeanName(beanName);
        targetSource.setBeanFactory(this.beanFactory);
        proxyFactory.setTargetSource(targetSource);
        Object proxy = proxyFactory.getProxy(this.beanClassLoader);
        ObjectName objectName = getObjectName(proxy, beanKey);
        if (logger.isDebugEnabled()) {
            logger.debug("Located MBean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
        }
        doRegister(proxy, objectName);
        return objectName;
    } else {
        // A simple bean... Let's create a lazy-init ModelMBean proxy with notification support.
        NotificationPublisherAwareLazyTargetSource targetSource = new NotificationPublisherAwareLazyTargetSource();
        targetSource.setTargetBeanName(beanName);
        targetSource.setBeanFactory(this.beanFactory);
        proxyFactory.setTargetSource(targetSource);
        Object proxy = proxyFactory.getProxy(this.beanClassLoader);
        ObjectName objectName = getObjectName(proxy, beanKey);
        if (logger.isDebugEnabled()) {
            logger.debug("Located simple bean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
        }
        ModelMBean mbean = createAndConfigureMBean(proxy, beanKey);
        targetSource.setModelMBean(mbean);
        targetSource.setObjectName(objectName);
        doRegister(mbean, objectName);
        return objectName;
    }
}
Also used : RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource) ObjectName(javax.management.ObjectName)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)78 Test (org.junit.Test)48 ITestBean (org.springframework.tests.sample.beans.ITestBean)20 TestBean (org.springframework.tests.sample.beans.TestBean)20 TimeStamped (org.springframework.tests.TimeStamped)8 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)8 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)8 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)5 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)4 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)4 Context (javax.naming.Context)3 TargetSource (org.springframework.aop.TargetSource)3 Advised (org.springframework.aop.framework.Advised)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 ObjectName (javax.management.ObjectName)2