Mój serwer nie ma sprężynowych etykiet AOP i nie mogę ich dodać. Wersja wiosna to 2.0.6.Jak utworzyć serwer proxy bez użycia sprężyny AOP
Chcę używać proxy dla 5 moich usług.
Jaki jest najlepszy sposób to zrobić
Mój serwer nie ma sprężynowych etykiet AOP i nie mogę ich dodać. Wersja wiosna to 2.0.6.Jak utworzyć serwer proxy bez użycia sprężyny AOP
Chcę używać proxy dla 5 moich usług.
Jaki jest najlepszy sposób to zrobić
Przykład stosując Wiosna fasoli postprocesor Proxy każdy 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();
}
}
}
Spójrz na java.lang.reflect.Proxy
API. Zauważ, że pozwala to na tworzenie serwerów proxy tylko dla interfejsów.
Dla HEADSTART: http://java.dzone.com/articles/power-proxies-java –
I ich stosowanych przy użyciu kodu java, ale nie wiem, jak przekazać proxy do fasoli sprężynowej bez korzystania z bibliotek proxy dostarczonych przez wiosnę – Mangoose
@manish, aby przekazać je do wiosny, utworzyć Spring FactoryBean, który zwraca proxy. – rees
Można wdrożyć proxy dynamiczne lub proxy CGLib.
Dziękuję za odpowiedź. Niestety moja implementacja beanfactory to DefaultListableBeanFactory, dlatego nie mogę używać BeanPostProcessor. – Mangoose
@manish bez problemu, dzięki za sprawdzenie! – rees