Ржевский Дмитрий Rzhevskiy Dmitry
Zk- injecting beans in components
Недавна вышла новая верся ZK - фреймворка для написания AJAX приложений.
Одной и наиболее понравившейся новой функциональности является Able to listen when a component is attached, detached.
С помощю этой функции я реализовал автоматический inject бинов из спринга.
1) я реализовал интерфейс UiLifeCycle:
/** Inject Spring variables to components. */
class AnnotationInjector implements UiLifeCycle {
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);
}
}
2) реализовал DesktopInit:
public class AnnotationInjectorInit implements DesktopInit {
public void init(Desktop desktop, Object request) throws Exception {
UiLifeCycle listener = new AnnotationInjector();
desktop.addListener(listener);
}
}
...
}
3) прописал в zk.xml
<listener>
<description>Inject spring beans in components.</description>
<listener-class>com.mycompany...ui.AnnotationInjectorInit</listener-class>
</listener>
Теперь можно в компонентах объявлять бины:
MyWindow exstends Window{
/** В это поле dao автоматически инжектится бин из спринга.*/
@Resource
MyDao mydao;
public void onCreate(){
//тут проинжектенный бин используется
myDao.find(..)
}
}
Всем енджоить!
PS похожую фичу, уже реализованной я видел в Wicket framework
UP чтобы действительно работало в методе onCreate (который вызывается до того как компонент пиаттачится) нужно слушать onCreateEvent:
public class OnCreateAnnotationInjector implements EventInterceptor {
/** Do nothing. */ public Event beforeSendEvent(Event event) { return event; }
/** For onCreate events call {@link #inject(Component)}. */
public Event beforePostEvent(Event event) {
if ("onCreate".equals(event.getName())) {
/** Do nothing. */ public Event beforeProcessEvent(Event event) { return event; }
/** Do nothing. */ public void afterProcessEvent(Event event) { }
/** Inject @Resource and @Bean fiels from spring to components. */
public void inject(Component comp) {
Field[] fields = comp.getClass().getDeclaredFields();
if (field.isAnnotationPresent(Resource.class)) {
beanid = field.getAnnotation(Resource.class).name();
if (field.isAnnotationPresent(Bean.class)) {
beanid = field.getAnnotation(Bean.class).value();
if (beanid == null) { continue; } field.setAccessible(true);
field.set(comp, SpringUtil.getApplicationContext().getBean(beanid));
} catch (IllegalAccessException e) {
zk.xml :
<listener>
<listener-class>..OnCreateAnnotationInjector</listener-class>
</listener>
Posted at 02:48PM июл 07, 2008 by Дима in Java | Комментарии[1]


опубликовал Дима Август 05, 2008 at 12:20 PM MSD #