📜 ⬆️ ⬇️

We build a web application in Java without JEE and Spring



This article will be interesting to those who suddenly realized that he wants
try to create something without JEE and Spring imposed by society.

You, oh dear Java Daredevil, do not want to be a cog in the huge mechanism of your Framework?
Are you tired of feeling like a “typist” in writing configs and settings?
You want to solve problems in the way you want , and not in the way that the author sees as unified for the needs
million libraries?
')
Then welcome to the cat, where I will share with you the useful finds of the World without the Framework.



It's all about the Container


As you remember, JEE or Spring (hereinafter referred to as the Framework) entices above all
the possibility of a container in which your small components and services will live.
But we can create it ourselves.

It's simple.
We create a context through which everything is available that is needed for work:
public class Context { public final DAO dao; public final MailService mails; public final AsyncService async; public App root; public Context( DAO dao, MailService mails, AsyncService async) { this.dao = dao; this.mails = mails; this.async = async; } } 


We create a container in which all our Services will live:
 public class App { public final SecurityService security; public final CommentService comments; public final UserService users; public App(Context c) { c.root = this; this.security = new SecurityService(c); this.users = new UserService(c); this.comments = new CommentService(c); } } 


In the Services we have business methods.
Through the Context, they will be able to access the necessary resources (like a database or other Services).

Example:
 class CommentService { DAO dao; MailService mails; UserService users; public CommentService(Context c){ dao = c.dao; mails = c.mails; users = c.root.users; } public Result getComments(Req req){ List comments = dao.getComments(req); User user = users.getCurUser(); mails.sendMail(); } } 


It remains only to create one instance of the Container for the entire web application:
 public class SingleApp { private static App app; public static synchronized App get() { if(app != null) return app; //  DAO dao = new DAO(...); MailService mails = new MailService(...); AsyncService async = new AsyncService(...); Context c = new Context(dao, mails, async); //  app = new App(c); return app; } } 


And use the Services from the Container in your work:
 @WebServlet("/comments") public class GetCommentsServlet extends HttpServlet { CommentService comments; public void init() { App app = SingleApp.get(); comments = app.comments; } protected void doGet(HttpServletRequest req, HttpServletResponse resp) { List result = comments.getComments(...); req.setAttribute("comments", result); req.getRequestDispatcher("/WEB-INF/jsp/comments.jsp").forward(req, resp); } } 


Total

With two or three simplest classes, we managed to create conditions for comfortable work without the Framework.
We did not need 5 megabytes of libraries, our solution starts as quickly as possible (because it only has
what exactly we need), it is easy to run in unit tests.
But the main thing : we see the entire cycle of the application and can change it as we please.

Using this approach as an example, I created this small web service . And I was pleased with the results.
There is no JEE, there is no Spring. But there is everything that I need. :)

If this topic hooks readers, then I will continue to acquaint you with the world outside the Framework with pleasure.

Source: https://habr.com/ru/post/220713/


All Articles