Skip to content

10. Context

FrankHossfeld edited this page Jul 12, 2023 · 8 revisions

Application Context

Inside a Nalu application the context is the store of all shared application information.

The context will be instantiated by the framework and can not be overridden or set by the developer implementation with another instance!
It is injected in every controller, filter, handler, module and loader. All controllers, handlers, filters, module and the loader will have access to the same instance.

This is a good place to store information that should be available application wide.

The context is a required attribute of the @Application- and (in case of a multi module project) @Module-annotation.

There are two different ways of implementing the context depending on the type of the Nalu application:

  • a single module implementation

  • a multi module implementation

Single Module Implementation

Inside a single module implementation you can use any POJO you like. You only need to add the IsContext-interface. The simplest implementation of a context looks like that:

public class MyApplicationContext
  implements IsContext {
  
  private String attribute;
  
  public MyApplicationContext() {
  }
  
  public String getAttribute() {
    return this.attribute;
  }
  
  public void setAttribute(String attribute) {
    this.attirbute = attribute;
  }
}

To inform Nalu about the context, you have to set the attribute context of the Application-annotation:

@Application(loader = MyApplicationLoader.class,
             startRoute = "/application/search",
             context = MyApplicationContext.class)
interface MyApplication
  extends IsApplication {
}

In case you plan to add modules in the future, you should consider the use of the AbstractModuleContext-class.

See Multi Module Implementation for more information.

Multi Module Implementation

Inside a multi module implementation you need a different kind of the context. (See the Module wiki page for more information).