Revision history [back]

click to hide/show revision 1
initial version

answered 2014-03-17 09:36:19 +0800

chillworld gravatar image chillworld flag of Belgium

https://github.com/chillw...

This is cause you don't have contextAwareness.

Create a AbstractVM and extend your VM who need spring context.

Code of the Absract class : (for MVVM)

package your.package;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Executions;

public abstract class AbstractVM {

    private WebApplicationContext applicationContext;

    public AbstractVM() {
        this.autowire();
    }

    private void autowire() {
        this.getApplicationContext().getAutowireCapableBeanFactory()
                .autowireBean(this);
    }

    public ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            applicationContext = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(getServletContext());
        }
        return applicationContext;
    }

    public ServletContext getServletContext() {
        return Executions.getCurrent().getDesktop().getWebApp()
                .getServletContext();
    }

    public void setApplicationContext(WebApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

}

This is cause you don't have contextAwareness.springs context.

Create a AbstractVM and extend your VM who need spring context.

Code of the Absract class : (for MVVM)

package your.package;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Executions;

public abstract class AbstractVM {

    private WebApplicationContext applicationContext;

    public AbstractVM() {
        this.autowire();
    }

    private void autowire() {
        this.getApplicationContext().getAutowireCapableBeanFactory()
                .autowireBean(this);
    }

    public ApplicationContext getApplicationContext() {
        if (applicationContext == null) {
            applicationContext = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(getServletContext());
        }
        return applicationContext;
    }

    public ServletContext getServletContext() {
        return Executions.getCurrent().getDesktop().getWebApp()
                .getServletContext();
    }

    public void setApplicationContext(WebApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

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