0

Zk- injecting spring beans in ZK components

asked 2008-07-07 11:19:19 +0800

dmrzh2 gravatar image dmrzh2
6

updated 2008-07-08 04:53:03 +0800

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);
        }
    }

To use this injector simply declare field with @Resource annotation
MyWindow exstends Window{

/** this field will be automaticaly injected .*/

  @Resource 

  MyDao mydao;

  public void onCreate(){

    //injected bean can used there

    myDao.find(..)

  } 

}

See full post in russian and english languages

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2008-07-08 14:09:56 +0800

sousa1981 gravatar image sousa1981
573 4

Nice, very nice.

Marcos de Sousa

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2008-07-07 11:19:19 +0800

Seen: 450 times

Last updated: Jul 08 '08

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More