Working with Dependency Injection

Dependency injection is a very important design principle for working simply and at the same time with major security. It lets us write a very simple code to test. Java uses this principle since Java EE 5 through the CDI specifications.

The first Java frameworks to host this technology were the following:

  • Seam: Firstly, it was a set of utilities to work better with web applications. Next, it was discontinued and integrated in the Java EE 5 specifications; now it represents the main standard that a developer must use.
  • Spring: This was the first Java dependency injection framework born exclusively for complex Java applications. Now it is an evolved set of plugins where you can build anything through dependency injection. It remains an alternative to Java EE.

Contexts and Dependency Injection (CDI) is a set of annotations and services that make it easy for developers, when used together, to work with enterprise technologies in services, web applications and enterprise beans. CDI works fine with stateful objects too and also has many larger uses. It gives developers much flexibility to integrate various types of components in a loosely coupled but type safe way.

Weld 2.3 is the injection engine used by WildFly 10.1. We can consider it as the evolution of JBoss Seam because the idea was initiated by the same authors. All Java EE 7 interfaces and annotations we have seen are fully supported by Weld. It is integrated with other applications servers too, such as JBoss Enterprise Application Platform (EAP), GlassFish, Oracle WebLogic Server, and WebSphere Application Server.

Weld can be started standalone too, thanks to an external module called Weld SE. Of course, you can use it only as a pure injection engine. All the integration with enterprise components doesn't work outside the containers.

Weld has no interfaces and annotations of its own because the CDI specifications 1.2 provide a rather complete set of tools. So, you will never see in your application custom classes with a package name containing the weld name. However, it's the real meat of Weld, a very transparent engine fully compliant with Java EE.

The CDI specifications involved in Java EE 7 will be described in the next paragraphs. Here you can see what we will show:

  • All the annotations available in CDI and how to use them
  • All the bean types available in CDI
  • How to use CDI in standalone and inside the enterprise components
  • Available scopes and when use them
  • Java patterns used in CDI as interceptors and Decorators
  • Introduction to Weld and monitoring of the beans through Weld
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.172