如何使用WireVariable将Bean注入修复到ViewModel?

时间:2019-04-16 14:41:15

标签: java spring zk

我正在开发Web应用程序,并尝试注入@Service以使用@WireVariable查看模型。分配建立和部署,但是在ViewModel初始化期间,我的bean方法调用上出现NullPointerException。由于某种原因,我的豆没有被注入。

通过@Inject注入控制器的效果很好。

ViewModel

package ua.dynamicdemoadmin;

public class ClientProfilesListViewModel {

    @WireVariable
    SomeB someB;

    @Init
    public void init() {

        someB.someM(); //exception appears here
    }
}

春季环境

<beans xmlns:zkspc="http://www.zkoss.org/2008/zkspring/core"
xsi:schemaLocation=http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd">

<context:component-scan base-package="ua.dynamicdemoadmin"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=""/>
</bean>

<zkspc:zk-config/>
</beans>
来自here

web.xml标准,以及:

<servlet>
        <servlet-name>adminServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-context.xml</param-value>
        </init-param>
        <load-on-startup>4</load-on-startup>
    </servlet>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/sping-context.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

例外

ERROR org.zkoss.bind.impl.ParamCall -
java.lang.NullPointerException: null
        at ua.dynamicdemoadmin.ClientProfilesListViewModel.init(ClientProfilesListViewModel.java:37) ~[classes/:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_74]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_74]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_74]
        at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:149) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:112) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:78) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:338) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:58) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:201) [zkbind-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:991) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:931) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:889) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:776) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:836) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:794) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:740) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:461) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:369) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140) [zk-8.6.0.1.jar:8.6.0.1]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [servlet-api.jar:?]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [servlet-api.jar:?]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316) [catalina.jar:8.5.34]
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [servlet-api.jar:?]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) [spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [servlet-api.jar:?]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:470) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:395) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316) [catalina.jar:8.5.34]
        at org.zkoss.web.servlet.Servlets.forward(Servlets.java:915) [zweb-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.ExecutionImpl.forward(ExecutionImpl.java:276) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.ExecutionImpl.forward(ExecutionImpl.java:283) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:451) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:369) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:215) [zk-8.6.0.1.jar:8.6.0.1]
        at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:140) [zk-8.6.0.1.jar:8.6.0.1]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) [servlet-api.jar:?]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [servlet-api.jar:?]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [catalina.jar:8.5.34]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [catalina.jar:8.5.34]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [catalina.jar:8.5.34]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [catalina.jar:8.5.34]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [catalina.jar:8.5.34]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [catalina.jar:8.5.34]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [catalina.jar:8.5.34]
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) [catalina.jar:8.5.34]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [catalina.jar:8.5.34]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [catalina.jar:8.5.34]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-coyote.jar:8.5.34]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-coyote.jar:8.5.34]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-coyote.jar:8.5.34]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-coyote.jar:8.5.34]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-coyote.jar:8.5.34]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_74]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_74]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.34]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74]

1 个答案:

答案 0 :(得分:0)

要连接Spring管理的bean,您需要注册Spring变量解析器DelegatingVariableResolver。然后,您可以注释@WireVariable来连接Spring托管的bean。 docs

ViewModel

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class ClientProfilesListViewModel {

    @WireVariable
    SomeB someB;
}