Search in sources :

Example 16 with ProxyFactory

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

the class RemoteExporter method getProxyForService.

/**
	 * Get a proxy for the given service object, implementing the specified
	 * service interface.
	 * <p>Used to export a proxy that does not expose any internals but just
	 * a specific interface intended for remote access. Furthermore, a
	 * {@link RemoteInvocationTraceInterceptor} will be registered (by default).
	 * @return the proxy
	 * @see #setServiceInterface
	 * @see #setRegisterTraceInterceptor
	 * @see RemoteInvocationTraceInterceptor
	 */
protected Object getProxyForService() {
    checkService();
    checkServiceInterface();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.addInterface(getServiceInterface());
    if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor.booleanValue() : this.interceptors == null) {
        proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
    }
    if (this.interceptors != null) {
        AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
        for (int i = 0; i < this.interceptors.length; i++) {
            proxyFactory.addAdvisor(adapterRegistry.wrap(this.interceptors[i]));
        }
    }
    proxyFactory.setTarget(getService());
    proxyFactory.setOpaque(true);
    return proxyFactory.getProxy(getBeanClassLoader());
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) GlobalAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry) AdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry)

Example 17 with ProxyFactory

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

the class MBeanServerConnectionFactoryBean method createLazyConnection.

/**
	 * Creates lazy proxies for the {@code JMXConnector} and {@code MBeanServerConnection}
	 */
private void createLazyConnection() {
    this.connectorTargetSource = new JMXConnectorLazyInitTargetSource();
    TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource();
    this.connector = (JMXConnector) new ProxyFactory(JMXConnector.class, this.connectorTargetSource).getProxy(this.beanClassLoader);
    this.connection = (MBeanServerConnection) new ProxyFactory(MBeanServerConnection.class, connectionTargetSource).getProxy(this.beanClassLoader);
}
Also used : TargetSource(org.springframework.aop.TargetSource) AbstractLazyCreationTargetSource(org.springframework.aop.target.AbstractLazyCreationTargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 18 with ProxyFactory

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

the class ApplicationListenerMethodAdapterTests method invokeListenerInvalidProxy.

@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"));
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 19 with ProxyFactory

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

the class EventPublicationInterceptorTests method testExpectedBehavior.

@Test
public void testExpectedBehavior() throws Exception {
    TestBean target = new TestBean();
    final TestListener listener = new TestListener();
    class TestContext extends StaticApplicationContext {

        @Override
        protected void onRefresh() throws BeansException {
            addApplicationListener(listener);
        }
    }
    StaticApplicationContext ctx = new TestContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("applicationEventClass", TestEvent.class.getName());
    // should automatically receive applicationEventPublisher reference
    ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
    ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
    ctx.refresh();
    EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(0, interceptor);
    ITestBean testBean = (ITestBean) factory.getProxy();
    // invoke any method on the advised proxy to see if the interceptor has been invoked
    testBean.getAge();
    // two events: ContextRefreshedEvent and TestEvent
    assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2);
    TestListener otherListener = (TestListener) ctx.getBean("&otherListener");
    assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestEvent(org.springframework.context.event.test.TestEvent) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) TestListener(org.springframework.context.TestListener) Test(org.junit.Test)

Example 20 with ProxyFactory

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

the class LocalSlsbInvokerInterceptorTests method testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods.

@Test
public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception {
    Object retVal = new Object();
    LocalInterface ejb = mock(LocalInterface.class);
    given(ejb.targetMethod()).willReturn(retVal);
    String jndiName = "foobar";
    Context mockContext = mockContext(jndiName, ejb);
    LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
    ProxyFactory pf = new ProxyFactory(new Class<?>[] { BusinessMethods.class });
    pf.addAdvice(si);
    BusinessMethods target = (BusinessMethods) pf.getProxy();
    assertTrue(target.targetMethod() == retVal);
    verify(mockContext).close();
    verify(ejb).remove();
}
Also used : Context(javax.naming.Context) ProxyFactory(org.springframework.aop.framework.ProxyFactory) EJBLocalObject(javax.ejb.EJBLocalObject) Test(org.junit.Test)

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