Τρίτη 17 Ιανουαρίου 2023

Ninja Framework (vs Spring)

 





INTRODUCTION


Ninja is a Java web framework that is lightweight and easy to use.

It is a fully-fledged framework for web development, providing amongst others:


DEPENDENCY INJECTION

ROUTING

MVC 

REST

PERSISTENCE

TESTING

METRICS

LOGGING

I18N

HOT-DEPLOYMENT




ANALYSIS /COMPARISON



DEPENDENCY INJECTION


Ninja uses Google Guice for DI, which has some benefits over the Spring Core DI:

  • Spring still relies heavily on XML configuration, meaning that type safety and refactoring are compromised. It is hard to make automated IDE refactorings on XMLs and Java files. Also, types are not checked in XML. On the other hand, Guice DI is done only on Java files.
  • While Spring DI uses String identifiers to resolve dependency implementation conflicts (when a type has many implementations), which is error-prone, Guice uses @BindingAnnotation in order to remove the need for Strings.
  • Guice allows for constructor injection, with setter injection being discouraged, Last one makes it difficult to have all dependencies centralized and under control.
  • Guice permits source code to remain framework-agnostic, and even allows for coexistence with Spring. We don't need to annotate our classes with Spring stereotypes, thus codebase can be portable.


ROUTING


Ninja has built-in mechanism to support an easy and readable routing:

public class Routes implements ApplicationRoutes {

@Override
public void init(Router router) {

router.GET().route("/index").with(ApplicationController::index);
router.GET().route("/home").with(ApplicationController::home);
router.GET().route("/hello").with(ApplicationController::hello);
router.GET().route("/editUser").with(ApplicationController::editUser);
router.GET().route("/createUser").with(ApplicationController::createUser);
router.GET().route("/books").with(ApplicationController::getAllBooks);

}
}




PERSISTENCE


By default, Ninja core provides Hibernate for persistence, including, Hibernate Validator, Hibernate EntityManager and Hibernate C3P0 for connection pool management.

We can configure DB url and credentials for a H2 along with familiar jpa setup, for example:

application.conf


persistence.xml







LIGHTWEIGHT / EASY SETUP


Using the correct maven archetype, the main skeleton and some code are generated:


Being an integrated software stack, Ninja then provides all libraries for most requirements of a web app.

As it uses convention-over-configuration, development speeds up.

A SuperDevMode functionality is provided to make the dev experience better:

Just insert above dependency and run "mvn ninja:run"



MODES AND APPLICATION CONFIG


Application configuration file is like Spring's one:

We also can reference properties within properties:



Depending on the current mode (dev, test, prod) that app runs, it can load relevant configuration:

To enable modes:

When running test cases, by default Test mode is enabled








Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?