我正在尝试在我的servlet项目中使用JPA,但我无法这样做,所以我收到错误消息,我的存储库为空,我无法解决这个问题

问题描述 投票:0回答:1

这是我的servlet代码

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Configurable
public class DoGet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductRepository productRepository;


    public void init(ServletConfig config) throws ServletException {

super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TODO Auto-generated method stub


String search = "%"+request.getParameter("search")+"%";
List<Product> findByProductNameLike = productRepository.findByProductNameLike(search);

PrintWriter writer = response.getWriter();
writer.write("<h1>Hello World How are You<h1>");

for(Product i: findByProductNameLike)
{
    writer.write(i.getProductId());
    writer.write(i.getProductName());
    writer.write(i.getImgPath());
}

    writer.write("<h1>Done<h1>");
 }

}

这是我的产品存储库

package com.servlet;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, String>{
    
    public List<Product> findByProductNameLike(String produtName);

}

我尝试创建另一个类的对象,因为我认为 ProductRepository 是一个接口,所以我可能会收到错误。

我期待窗口上有正确的输出,但我在线上收到了错误消息:productRepository 为空

List<Product> findByProductNameLike = productRepository.findByProductNameLike(search);

这是正确的错误消息:

Type Exception Report

Message Cannot invoke "com.servlet.ProductRepository.findByProductNameLike(String)" because "this.productRepository" is null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException: Cannot invoke "com.servlet.ProductRepository.findByProductNameLike(String)" because "this.productRepository" is null
    com.servlet.DoGet.doGet(DoGet.java:40)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

我使用的是 Apache Tomcat v10.0。我已经查看了许多教程和其他问题的解决方案,但仍然无法以所需的方式运行我的代码。

由于垃圾邮件问题,我无法发布 applicationContext.xml 和 web.xml。

java spring jpa servlets spring-repositories
1个回答
0
投票

我认为问题在于 DoGet 不是 bean/组件。因此,添加 @Component 或 @Configuration (也许这是这里的拼写错误?),然后所有内容都应该正确自动装配。

© www.soinside.com 2019 - 2024. All rights reserved.