Understanding JBoss Seam Contextual Dependency Injection

 

WHY Contextual Dependency Injection?

  • Dependency injection promotes loose coupling design pattern. Traditionally, the client is responsible for creating the dependent object or looking up a service.  Therefore, it creates a tight coupling between the client and dependent object.
  • Seam allows developers to inject Seam container managed object into Seam component.  Seam container is responsible for creating and managing the life cycle of the object.  Therefore, the client is not dependent on the implementation of the object.
  • Each object managed by Seam has a well defined scope that is bound to a specific context. Seam extends servlet context model.

 

How does Seam Inject work?

dependent injection.bmp

 

 

  • Client calls the register method.
  • Seam intercepts the call and injects user object and Entity manager just before invoking the register method.
  • Seam outject components annotated with “@Out”
  • Seam reset the injected attribute to null to get ready for next call.

 

Seam context scope

scope.bmp

 

 

 

  • Event scoped components last from the beginning of the JSF request to the end of request.
  • Default conversion scoped component last from the beginning of the JSF request to the end of request.   Default conversion scope component survives page redirect.
  • Page scoped component lasts from the beginning of the JSF request until the user leaves the page.   Page scoped components are stored in the JSF component tree.

 

 

Component life cycle management

life cycle.bmp

 

  • @In(create=true) tells Seam container to create a component instance if it does not exists in the context
  • @Startup tells Seam container to create a component at initialization time
  • @AutoCreate tells Seam container to create a component automatically
  • @Factory tells Seam container to initialize the component using method annotated if the component does not exist in the context
  • @Out tells seam to outject a component to the context
  • @Create tells Seam to call this method after instantiating an object