Example usage for org.springframework.aop.framework ProxyFactory setProxyTargetClass

List of usage examples for org.springframework.aop.framework ProxyFactory setProxyTargetClass

Introduction

In this page you can find the example usage for org.springframework.aop.framework ProxyFactory setProxyTargetClass.

Prototype

public void setProxyTargetClass(boolean proxyTargetClass) 

Source Link

Document

Set whether to proxy the target class directly, instead of just proxying specific interfaces.

Usage

From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java

public static Object createProxy(Object objectToProxy, ApplicationService advice, ProxyHelper proxyHelper) {
    ProxyFactory pf = new ProxyFactory(objectToProxy);
    pf.setProxyTargetClass(true);
    pf.addAdvice(new LexEVSBeanProxy(advice, proxyHelper));
    pf.setExposeProxy(true);/* w  w w.  ja  v a  2s.c o  m*/
    return pf.getProxy();
}

From source file:org.jdal.aop.SerializableProxyUtils.java

/**
 * Create a new Serializable proxy for the given target.
 * @param target target to proxy//  w w w. ja v a 2 s.  co  m
 * @param reference serializable reference 
 * @return a new serializable proxy
 */
private static Object createSerializableProxy(Object target, SerializableReference reference) {
    ProxyFactory pf = new ProxyFactory(target);
    pf.setExposeProxy(true);
    pf.setProxyTargetClass(reference.isProxyTargetClass());
    pf.addInterface(SerializableObject.class);
    pf.addAdvice(new SerializableIntroductionInterceptor(reference));

    return pf.getProxy(reference.getBeanFactory().getBeanClassLoader());
}

From source file:net.shopxx.service.impl.ConfigServiceImpl.java

public void init() {
    try {/*from   w w  w  .  j a  v  a2 s . co m*/
        Setting setting = SystemUtils.getSetting();
        setting.setSmtpPassword(null);
        setting.setKuaidi100Key(null);
        setting.setCnzzPassword(null);
        setting.setSmsKey(null);
        ProxyFactory proxyFactory = new ProxyFactory(setting);
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.addAdvice(new MethodBeforeAdvice() {

            public void before(Method method, Object[] args, Object target) throws Throwable {
                if (StringUtils.startsWith(method.getName(), "set")) {
                    throw new UnsupportedOperationException("Operation not supported");
                }
            }

        });
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.setSharedVariable("setting", proxyFactory.getProxy());
        configuration.setSharedVariable("locale", setting.getLocale());
        configuration.setSharedVariable("theme", setting.getTheme());
        if (setting.getIsDevelopmentEnabled()) {
            configuration.setSetting("template_update_delay", "0");
            reloadableResourceBundleMessageSource.setCacheSeconds(0);
        } else {
            configuration.setSetting("template_update_delay", templateUpdateDelay);
            reloadableResourceBundleMessageSource.setCacheSeconds(messageCacheSeconds);
        }
        fixedLocaleResolver.setDefaultLocale(LocaleUtils.toLocale(setting.getLocale().toString()));
    } catch (TemplateModelException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (TemplateException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.ext4spring.parameter.ParameterBeanFactory.java

@Override
public Object getObject() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory(parameterBean);
    proxyFactory.addAdvice(parameterAdvice);
    proxyFactory.setProxyTargetClass(!parameterBean.getClass().isInterface());
    return proxyFactory.getProxy();
}

From source file:fr.pilato.spring.elasticsearch.ElasticsearchNodeFactoryBean.java

@Override
public void afterPropertiesSet() throws Exception {

    if (async) {//from  w w w.jav a  2 s . co m
        Assert.notNull(taskExecutor);

        Future<Node> nodeFuture = taskExecutor.submit(new Callable<Node>() {
            @Override
            public Node call() throws Exception {
                return initialize();
            }
        });

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setProxyTargetClass(true);
        proxyFactory.setTargetClass(Node.class);
        proxyFactory.addAdvice(new GenericInvocationHandler(nodeFuture));
        proxyfiedNode = (Node) proxyFactory.getProxy();

    } else {
        node = initialize();
    }
}

From source file:serialization.ProxySerializationTest.java

@Test
public void testSerializableTargetSource() {
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(new SerializableTargetSource(beanFactory, "b2", true));
    pf.setProxyTargetClass(true);
    pf.addInterface(Serializable.class);
    serialize(pf.getProxy());/*from   ww  w  . j  a  v a 2 s  .c  o m*/
}

From source file:net.sf.oval.test.integration.spring.SpringAOPAllianceBeanValidationTest.java

public void testJDKProxying() {
    final ProxyFactory prFactory = new ProxyFactory(new TestServiceWithInterface());
    prFactory.setProxyTargetClass(false);
    prFactory.addAdvice(new GuardInterceptor(new Guard(new BeanValidationAnnotationsConfigurer())));
    final TestServiceInterface testServiceWithInterface = (TestServiceInterface) prFactory.getProxy();

    try {//from  w  ww  .  jav  a 2s  .  co  m
        testServiceWithInterface.getSomething(null);
        fail();
    } catch (final ConstraintsViolatedException ex) {
        assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
    }

    try {
        testServiceWithInterface.getSomething("123456");
        fail();
    } catch (final ConstraintsViolatedException ex) {
        assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
    }
}

From source file:savetheenvironment.refresh.RefreshableFactoryBean.java

@Override
@SuppressWarnings("unchecked")
public T getObject() throws Exception {

    if (null == this.atomicReference.get()) {
        updateBeanReference();//  w ww.  jav a  2  s  .  c o m
    }

    T beanReference = atomicReference.get();

    ProxyFactory pf = new ProxyFactory(beanReference);
    pf.setProxyTargetClass(true);
    List<Class<?>> listOfClasses = new ArrayList<Class<?>>();
    listOfClasses.add(Refreshable.class);
    listOfClasses.add(ApplicationListener.class);
    listOfClasses.add(DisposableBean.class);
    listOfClasses.addAll(Arrays.asList(beanReference.getClass().getInterfaces()));
    pf.setInterfaces(listOfClasses.toArray(new Class[listOfClasses.size()]));
    pf.addAdvice(new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            Method method = invocation.getMethod();
            // if someone externally calls refresh, then rebuild the reference
            if (method.equals(refreshMethod)) {
                updateBeanReference();
                return atomicReference.get();
            } else if (method.equals(disposeMethod)) {
                T atomicT = atomicReference.get();
                if (atomicT instanceof DisposableBean) {
                    ((DisposableBean) atomicT).destroy();
                }
            } else if (method.equals(onApplicationEventMethod)) {
                Object[] arguments = invocation.getArguments();
                if (arguments[0] instanceof RefreshEvent) {
                    updateBeanReference();
                }
                return null;
            }
            // otherwise send the request on through to the underlying object unchanged
            Object ref = atomicReference.get();
            return method.invoke(ref, invocation.getArguments());

        }
    });
    Object o = pf.getProxy();
    return (T) o;
}

From source file:net.sf.oval.test.integration.spring.SpringAOPAllianceBeanValidationTest.java

public void testCGLIBProxying() {
    {/*from   w  w w.ja va  2 s. c o  m*/
        final ProxyFactory prFactory = new ProxyFactory(new TestServiceWithoutInterface());
        prFactory.setProxyTargetClass(true);
        prFactory.addAdvice(new GuardInterceptor(new Guard(new BeanValidationAnnotationsConfigurer())));
        final TestServiceWithoutInterface testServiceWithoutInterface = (TestServiceWithoutInterface) prFactory
                .getProxy();

        try {
            testServiceWithoutInterface.getSomething(null);
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
        }

        try {
            testServiceWithoutInterface.getSomething("123456");
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
        }
    }

    {
        final ProxyFactory prFactory = new ProxyFactory(new TestServiceWithInterface());
        prFactory.setProxyTargetClass(true);
        prFactory.addAdvice(new GuardInterceptor(new Guard(new BeanValidationAnnotationsConfigurer())));
        final TestServiceWithInterface testServiceWithInterface = (TestServiceWithInterface) prFactory
                .getProxy();

        try {
            testServiceWithInterface.getSomething(null);
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("NOT_NULL", ex.getConstraintViolations()[0].getMessage());
        }

        try {
            testServiceWithInterface.getSomething("123456");
            fail();
        } catch (final ConstraintsViolatedException ex) {
            assertEquals("MAX_LENGTH", ex.getConstraintViolations()[0].getMessage());
        }
    }
}

From source file:aop.DeclareMixinAspectJAdvisorFactoryTest.java

protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
    ProxyFactory pf = new ProxyFactory(target);
    if (interfaces.length > 1 || interfaces[0].isInterface()) {
        pf.setInterfaces(interfaces);//from  www. j  a va2 s  .  co m
    } else {
        pf.setProxyTargetClass(true);
    }

    // Required everywhere we use AspectJ proxies
    pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);

    for (Object a : advisors) {
        pf.addAdvisor((Advisor) a);
    }

    pf.setExposeProxy(true);
    return pf.getProxy();
}