CanDI Pattern Tutorial: SetServlet

The SetServlet demonstrates the four Java Injection patterns by injecting the services and resources and displaying their values in tables.

Click on the 'SetServlet blue' link, the 'SetServlet red' link, and the 'SetServlet' link to check that both servlets share the same MyService instance

Demo Links

Service Pattern

BindingValue
@Inject MyServiceMyServiceBean[MyStartupBean: initial value]

Resource Pattern

BindingValue
@Red MyResourceRedResourceBean[red resource]
@Blue MyResourceBlueResourceBean[blue resource]

Plugin/Extension Pattern

  1. BlueResourceBean[blue resource]
  2. GreenResourceBean[default]
  3. RedResourceBean[red resource]
  4. StartupResourceBean[MyStartupBean: initial value]

Startup Pattern

BindingValue
@Inject StartupResourceBeanStartupResourceBean[MyStartupBean: initial value]

Demo Architecture

SetServlet Code

public class SetServlet extends HttpServlet {
  // service pattern
  private @Inject MyService _service;

  // resource pattern
  private @Red MyResource _redService;
  private @Blue MyResource _blueService;

  // plugin/extension pattern
  private @Any Instance<MyResource> _resources;

  // startup pattern
  private @Inject StartupResourceBean _startupResource;

  ...
}