使用 jpa 存储库时的 Zks Servlet.service() NPE

Zks Servlet.service() NPE when using a jpa repository

我目前遇到了一个令人困惑的令人沮丧的问题。

我正在启动 Spring Boot + ZK webapp。到目前为止,我进行了安全登录,根据登录用户的角色重定向到不同的页面。此数据存储在我的 MySQL 数据库中,我使用 JPA 访问该数据库。由于这个和一些早期的测试,我知道我的配置在 ZK、Spring 和 JPA 之间按预期工作。

现在我正在尝试显示超级用户角色的用户列表。为此,我使用默认的命名查询 findAll(),但这正是我遇到问题的地方。如果我尝试在我的 ViewModel class 中使用我的 repo,我会在我使用所述 repo 的那一行得到一个 NullPointerException。查询本身没有被调用,它在找到 repo 时停止。

阅读了很多其他案例后,我得出的结论是我的配置没问题(加上我已经有我的 repo 用于登录),所以必须有另一个问题,这将我指向了 DHtmlLayoutServlet . STS 抛出 2 个错误堆栈:

堆栈 1:

ERROR org.zkoss.bind.impl.ParamCall - 
java.lang.NullPointerException: null
    at com.project.controllers.su.SuperUserPanelVM.init(SuperUserPanelVM.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:172)
    at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:112)
    at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:78)
    at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:333)
    at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:58)
    at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:205)
    at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:999)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:939)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:897)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:784)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:844)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:802)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:748)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:469)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:377)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:327)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:81)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:147)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:218)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:110)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:211)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

堆栈 2

org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet [DHtmlLayoutServlet] lanzó excepción
java.lang.NullPointerException
    at com.project.controllers.su.SuperUserPanelVM.init(SuperUserPanelVM.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:172)
    at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:112)
    at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:78)
    at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:333)
    at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:58)
    at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:205)
    at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:999)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:939)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:897)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:784)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:844)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:802)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:748)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:469)
    at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:377)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:220)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:146)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:327)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:81)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:147)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:218)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:110)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:211)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

据我猜测,问题出在 ZKs Servlet 上……但为什么它在使用独立组件的同时这样做呢?当我还没有使用数据时,JPA 接口怎么会抛出这个异常 我很困惑。到目前为止,我已经尝试了应该做同样事情的不同配置,尝试了不同的注释,例如 @WireVariable、@Resource 和 @Autowired,我的自定义 LoginSuccessHandler 和我的 SuperUserPanelVM(Resource 和 Autowired 工作正常。WireVariable 没有,但是我没想到它能正常工作)并检查了非托管依赖项。

数据库中有数据。我正在查询 Users/Usuarios,它有 2 条记录。此 table 中没有可为空的值。即使有一个空结果集,因为我没有对它做任何事情,它也不应该停止,所以至少我知道当我尝试使用 repo 时它不喜欢。

这是我的(相关部分)代码:

SuperUserPanel.java

public class SuperUserPanelVM {
    
    //(@WireVariable/@Resource/@Autowired) -> none worked, neither without annotations
    private UsuariosRepository usuariosRepository;
    
    @Init
    public void init() {
        usuariosRepository.findAll(); // -> It explodes here
    }
}

UsuariosRepository.java

@Repository
public interface UsuariosRepository extends JpaRepository<Usuarios, Integer>{

    Usuarios findByCorreo(String correo); // This is used for the login
}

Usuarios.java

@Entity
@Table(name="usuarios")
@NamedQuery(name="Usuarios.findAll", query="SELECT u FROM Usuarios u")
public class Usuarios {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_usuario")
    private int idUsuario;
    
    @Column(name="correo")
    private String correo;
    
    @Column(name="nombre")
    private String nombre;
    
    @Column(name="apellido_paterno")
    private String apellidoPaterno;
    
    @Column(name="apellido_materno")
    private String apellidoMaterno;
    
    @Column(name="telefono")
    private String telefono;
    
    @Column(name="password")
    private String password; //This is going to be further worked for stronger security, dw
    
    @Column(name="rol")
    private String rol;

    
    /* SETTES & GETTERS*/
}

DatabaseAuthProvider.java(回购工作的地方)

@Component
public class DatabaseAuthProvider implements AuthenticationProvider {
    
    @Resource
    private UsuariosRepository usuariosRepository;
    
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

    @Override
    public Authentication authenticate(Authentication auth) throws AuthenticationException {
        // check creds + redirect 
    }
}

我唯一引用 DHtmlLayoutServlet 的地方是在注册所述 servlet 时:

ProjectWebConfig.java

@Configuration
public class ProjectWebConfig implements WebApplicationInitializer{
    
    private static final Class<?>[] configurationClasses = new Class<?>[] {Config.class, Security.class};
    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        registerListener(servletContext);   
        registerDispatcherServlet(servletContext);
        registerSpringSecurityFilterChain(servletContext);
    }
    
    private void registerListener(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext rootContext = createContext(configurationClasses);
        servletContext.addListener(new ContextLoaderListener(rootContext));
        servletContext.addListener(new RequestContextListener());       
        servletContext.addListener(new HttpSessionListener());
    }
        
    private void registerDispatcherServlet(ServletContext servletContext) {
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(Config.class);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("zkLoader", new DHtmlLayoutServlet());

        dispatcher.setInitParameter("update-uri", "/zkau");
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.zul");
        
        dispatcher = servletContext.addServlet("auEngine", new DHtmlUpdateServlet());
        dispatcher.addMapping("/zkau/*");
                
    }
    
    private void registerSpringSecurityFilterChain(ServletContext servletContext) {
        //...
    }
    
    private AnnotationConfigWebApplicationContext createContext(final Class<?>... annotatedClasses) {
       //...
    }
}

我想我忽略了一些重要的事情,但我是那种在沮丧时会失去注意力的人。另一方面,ProjectWebConfig.java 中的 Config.class 具有带有正确 URI 的 @ComponentScan、@EnableJpaRepositories 和 @EntityScan 注释。

我只需要这个就可以了。在其他类似的项目中,同样的配置和架构工作得很好。我几乎正在使用具有相同技术的其他项目的蓝图,但我只是不知道这个错误是怎么回事。我不知道是 ZK 的 servlet 做的,还是 JPA 做的,两者的混合……总之,感谢您阅读到这里。我真的希望有人能指出我正确的方向。如果需要更多详细信息,我会尽快提供。祝你有个愉快的一天。

旁注:我编辑了 class 名称。如果你发现它有什么不对,我会在我的代码片段中确认它是否有误。

由于您没有显示相关的 zul 代码,我假设您正在按 class 名称应用视图模型。

<window viewModel="@id('vm') @init('my.package.SuperUserPanelVM')" ...

这意味着视图模型实例是由 ZK 而不是 Spring 创建的(这排除了使用 spring 的注释 @Resource/@Autowired , 前面已经提到了 ).

因此,如果 ZK 创建视图模型,则 @WireVariable 是要使用的注释(只是试图减少此处的 problem/solution space)。

@WireVariable
private UsuariosRepository usuariosRepository;

@WireVariable 将 'search' 用于变量名称为 usuariosRepository 的 bean(我假设它有效,但是......它不会查看 Spring 应用程序内部默认上下文)

为此,您必须在视图模型 class 中指定 @VariableResolver 以将 spring beans 包含到变量解析中。 (我猜你忘了)。

import org.zkoss.zkplus.spring.DelegatingVariableResolver

@VariableResolver(DelegatingVariableResolver.class)
public class SuperUserPanelVM {
  @WireVariable
  private UsuariosRepository usuariosRepository;

现在@WireVariable 应该在应用程序上下文中找到存储库。为了确保只在 DelegatingVariableResolver 中添加一个断点来验证这个变量解析器被调用并且你的 spring 应用程序上下文实际上包含一个名为 'usuariosRepository'.

的 bean

为了更加确保您可以为您的存储库指定一个明确的名称(只是为了确保没有人重新配置默认的 bean 命名约定):

@Repository("usuariosRepository")
public interface UsuariosRepository extends JpaRepository<Usuarios, Integer>{

这是我们演示项目中的一个工作示例:

https://github.com/zkoss/zkspringboot/blob/master/zkspringboot-demos/zkspringboot-demo-jar/src/main/java/org/zkoss/zkspringboot/demo/viewmodel/MainViewModel.java#L16-L20


只是为了完整性(不是试图混淆):

还有一种方法可以让 spring 创建您的视图模型实例,然后 spring 特定注释可用 @Autowired/@Resource。如果需要,请尝试具体说明您的问题。