How to create a proxy without using spring AOP

My server does not have spring AOP banks, and I cannot add them. spring version 2.0.6.

I want to use proxies for 5 of my services.

What is the best way to do this

+6
source share
3 answers

An example of using the Spring bean post processor for a proxy server every bean:

public class ProxifyingPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { Class beanClass = bean.getClass(); if (Proxy.isProxyClass(beanClass)) { return bean; } List<Class<?>> interfaceList = getAllInterfaces(beanClass); Class[] interfaces = (interfaceList.toArray(new Class[interfaceList.size()])); return Proxy.newProxyInstance(beanClass.getClassLoader(), interfaces, new InvocationHandler() { @Override public Object invoke(Object o, Method method, Object[] objects) throws Throwable { return method.invoke(bean, objects); } }); } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } private List<Class<?>> getAllInterfaces(Class<?> cls) { if (cls == null) { return null; } LinkedHashSet<Class<?>> interfacesFound = new LinkedHashSet<Class<?>>(); getAllInterfaces(cls, interfacesFound); return new ArrayList<Class<?>>(interfacesFound); } private void getAllInterfaces(Class<?> cls, HashSet<Class<?>> interfacesFound) { while (cls != null) { Class<?>[] interfaces = cls.getInterfaces(); for (Class<?> i : interfaces) { if (interfacesFound.add(i)) { getAllInterfaces(i, interfacesFound); } } cls = cls.getSuperclass(); } } } 
+1
source

Take a look at the java.lang.reflect.Proxy API. Please note that it allows you to create proxies only for interfaces.

+1
source

You can implement Dynamic Proxies or CGLib proxies.

+1
source

All Articles