如何使用 Servlet 的依赖项?

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

我有一个业务逻辑层,我想在 servlet 中使用它,但我所做的任何事情都会抛出一个错误:

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       IPersonService service;
 
    public Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init(){
        service=new PersonService();
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service.getAll();
    }

PersonService 来自外部项目,我有参考。我在自己的项目中测试它并且它有效,但是当我想在 servlet 中使用它时,它给了我一个奇怪的错误:

Caused by: java.lang.NoClassDefFoundError: LBusinessInterfaces/IPersonService;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Unknown Source)
    at java.lang.Class.getDeclaredFields(Unknown Source)
    at org.apache.catalina.util.Introspection.getDeclaredFields
java servlets dependency-injection three-tier
1个回答
1
投票

您需要拥有包含业务接口(PersonService)的外部依赖 jar。到 servlet 容器中。应该是容器的“lib”文件夹,或者应该在项目的“WEB-INF/lib”文件夹中找到。

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