Hura's Web setup combines the injection functionality of the core with the Java Servlet 4 specification.
To launch a web application injected by Hura, implement the interface HuraWebApplicationInitializer, which...
- is marked as a ServletContainerInitializer, so it will be picked up by servlet container providers
- extends Hura's Blueprint to allow defining the web environment of the application using dependency injection
public class SomeWebApplication implements HuraWebApplicationInitializer {
// define stuff here
}
The HuraWebApplicationInitializer implementation is injected by a RootInjector by Hura Web, allowing the beans defined by the Blueprint to retrieve an Injector instance injected by the applications root injection sequence.
From there, the beans can build up and tear down the application arbitrarily in as many sub sequences as desired when requests are retrieved by components of the application's entities.
As mentioned before, the HuraWebApplicationInitializer is being injected by a RootInjector on application startup.
When the servlet container provider shuts down, that RootInjector is shut down by Hura Web. As a result, the initializer and all beans originating from it will be destroyed automatically when the application shuts down, effectively cleanly ending their natural bean life cycle.
The environment of a web application consists of servlet specification entities registered as the container provider, with the main entity being the Servlet.
Hura web allows such entities to be defined by creating singleton beans implementing ServletContextConfiguration. The interface can be implemented freely, but the WebEnvironmentFactory provides implementations for standard use cases.
SingletonAllocation instances defined by a HuraWebApplicationInitializer implementation holding a bean implementing ServletContextConfiguration will be automatically registered with the servlet container provider:
public class SomeWebApplication implements HuraWebApplicationInitializer {
@Define
public Blueprint.SingletonAllocation defineServlet() {
return WebEnvironmentFactory.registerServlet(SomeServlet.class).build();
}
}