Search in sources :

Example 36 with ProxyFactory

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

the class FormattingConversionServiceTests method proxiedConverter.

@Test
public void proxiedConverter() {
    Converter<?, ?> converter = new IntegerConverter();
    formattingService.addConverter((Converter<?, ?>) new ProxyFactory(converter).getProxy());
    assertEquals(Integer.valueOf(1), formattingService.convert("1", Integer.class));
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.Test)

Example 37 with ProxyFactory

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

the class MBeanExporterTests method testExportJdkProxy.

@Test
public void testExportJdkProxy() throws Exception {
    JmxTestBean bean = new JmxTestBean();
    bean.setName("Rob Harrop");
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(bean);
    factory.addAdvice(new NopInterceptor());
    factory.setInterfaces(IJmxTestBean.class);
    IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
    String name = "bean:mmm=whatever";
    Map<String, Object> beans = new HashMap<>();
    beans.put(name, proxy);
    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.registerBeans();
    ObjectName oname = ObjectName.getInstance(name);
    Object nameValue = server.getAttribute(oname, "Name");
    assertEquals("Rob Harrop", nameValue);
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) HashMap(java.util.HashMap) IJmxTestBean(org.springframework.jmx.IJmxTestBean) JmxTestBean(org.springframework.jmx.JmxTestBean) IJmxTestBean(org.springframework.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 38 with ProxyFactory

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

the class DefaultDbRefResolver method createLazyLoadingProxy.

/**
 * Creates a proxy for the given {@link MongoPersistentProperty} using the given {@link DbRefResolverCallback} to
 * eventually resolve the value of the property.
 *
 * @param property must not be {@literal null}.
 * @param dbref can be {@literal null}.
 * @param callback must not be {@literal null}.
 * @return
 */
private Object createLazyLoadingProxy(MongoPersistentProperty property, @Nullable DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler handler) {
    Class<?> propertyType = property.getType();
    LazyLoadingInterceptor interceptor = new LazyLoadingInterceptor(property, dbref, exceptionTranslator, callback);
    if (!propertyType.isInterface()) {
        Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(propertyType));
        factory.setCallbacks(new Callback[] { interceptor });
        return handler.populateId(property, dbref, factory);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    for (Class<?> type : propertyType.getInterfaces()) {
        proxyFactory.addInterface(type);
    }
    proxyFactory.addInterface(LazyLoadingProxy.class);
    proxyFactory.addInterface(propertyType);
    proxyFactory.addAdvice(interceptor);
    return handler.populateId(property, dbref, proxyFactory.getProxy());
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 39 with ProxyFactory

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

the class SessionAwareMethodInterceptorUnitTests method createProxyInstance.

private <T> T createProxyInstance(ClientSession session, T target, Class<T> targetType) {
    ProxyFactory factory = new ProxyFactory();
    factory.setTarget(target);
    factory.setInterfaces(targetType);
    factory.setOpaque(true);
    factory.addAdvice(new SessionAwareMethodInterceptor<>(session, target, MongoDatabase.class, this::proxyDatabase, MongoCollection.class, this::proxyCollection));
    return targetType.cast(factory.getProxy());
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 40 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project uPortal by Jasig.

the class EventingLocalContainerEntityManagerFactoryBean method createNativeEntityManagerFactory.

@Override
protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException {
    final String persistenceUnitName = this.getPersistenceUnitName();
    // Create actual EMF
    final EntityManagerFactory nativeEntityManagerFactory = super.createNativeEntityManagerFactory();
    // Add a proxy to the EMF which results in events
    final ProxyFactory proxyFactory = new ProxyFactory(nativeEntityManagerFactory);
    proxyFactory.addAdvice(new EventingEntityMangerFactoryInterceptor(applicationEventPublisher, contextReady, persistenceUnitName));
    return (EntityManagerFactory) proxyFactory.getProxy();
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)81 Test (org.junit.Test)49 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 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 Factory (org.springframework.cglib.proxy.Factory)3 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2