我需要在没有依赖项注入框架的情况下实现servlet 3.0 webapp。最佳解决方案是哪种? (就性能/内存使用情况和可伸缩性而言)

1)DAO和服务层的静态方法

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        MyService.doSomething(/*args*/);
    }
}

public class MyService {

    public static void doSomething(/*args*/) {
        MyDAO.crud(/*args*/);
    }
}

public class MyDAO {

    public static void crud(/*args*/) {
        //...
    }
}


2)接口

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    private MyService myService;

    public MyController() {
        super();
        if (myService != null) {
            myService = new MyServiceImpl();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        myService.doSomething(/*args*/);
    }
}

public interface MyService {

    void doSomething(/*args*/);
}

public class MyServiceImpl implements MyService {

    private MyDAO myDAO;

    public MyServiceImpl() {
        if (myService != null) {
            myDAO = new MyDAOImpl();
        }
    }

    @Override
    public void doSomething(/*args*/) {
        myDAO.crud(/*args*/);
    }
}

public interface MyDAO {

    void crud(/*args*/);
}

public class MyDAOImpl implements MyDAO {

    @Override
    public void crud(/*args*/) {
        //...
    }
}


3)其他...

谢谢。

最佳答案

最好在MyService中将MyDAOapplication都设置为stateless
您可以将它们都设置为singletons(这是注入的所有defaultspring作用域beans的任何方式),只需在controller中使用它即可。由于它的stateless,所以concurrency问题没有问题。

07-28 13:33