-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Very intresting feature I find in ZK 2.0.6: Able to listen when a component is attached, detached.
With this feature I implement automatic spring beans injecting.
I impement interface UiLifeCycle:
class AnnotationInjector implements UiLifeCycle { /** Inject @Resource fiels from spring to components. */ public void afterComponentAttached(Component comp, Page page) { Field[] fields = comp.getClass().getDeclaredFields(); for (Field field : fields) { if (!field.isAnnotationPresent(Resource.class)) { continue; } String beanid = field.getAnnotation(Resource.class).name(); if (beanid.equals("")) { beanid = field.getName(); } field.setAccessible(true); try { field.set(comp, SpringUtil.getApplicationContext().getBean(beanid)); } catch (IllegalAccessException e) { log.error("can't set value", e); } field.setAccessible(false); } }
MyWindow exstends Window{ /** this field will be automaticaly injected .*/ @Resource MyDao mydao; public void onCreate(){ //injected bean can used there myDao.find(..) } }
Asked: 2008-07-07 11:19:19 +0800
Seen: 450 times
Last updated: Jul 08 '08