Please see the second part here
Contexts and Dependency Injection (CDI ) JSR 299 is a new feature in Java EE 6 Web Profile . Its Enhance the JSR 330 Dependency Injection in Java. CDI are highly used annotation. CDI makes Loose coupling between classes. To use javax.inject.Inject annotation, we can inject any class into another classes
In traditional way, we create the instance using 'new' operator in another class. here thats instance life time is dependent on target class. its tightly coupled. Here CDI makes to loosely coupled and change the life-cycle for that instance.
CDI provides
Dependency Injection is one of the great feature in Spring. but there used String based DI. here finding problems (i.e string misspelling, Incompatible classes) are in runtime only. In CDI uses annotation its find most of the problem in compile time.
Traditional Way
In ClassB class we create ClassA instance using 'new' operator and life-cycle of ClassA is bounded. But if we use CDI then instance creation responsibility goes to container (Java EE Container)
CDI Implementations
In Weld is integrated in GlassFish server 3.0 by default. When we create project in Netbeans we need to check the Enable Context and Dependency Injection Checkbox in Project Creation wizard, Netbeans automatically create beans.xml in WEB-INF Folder . Otherwise CDI is not enabled. If u use old netbeans (older than Netbeans 6.9) then that option is not present, so u need to manualy create empty beans.xml file in WEB-INF folder. If beans.xml file is present then CDI is enabled, Otherwise its disabled.
Example
Tester.java
here we are not using any 'new' operator. Container take the responsibility to create the instance where its needed. If u inject the interface, container automatically find the implementaion of the interface and inject it.
If we have more than one implementation of particular interface then its show ambiguity exception in compile time. because container confuse which one should inject?. We solve the problem in two ways.
Please see the second part here
Contexts and Dependency Injection (CDI ) JSR 299 is a new feature in Java EE 6 Web Profile . Its Enhance the JSR 330 Dependency Injection in Java. CDI are highly used annotation. CDI makes Loose coupling between classes. To use javax.inject.Inject annotation, we can inject any class into another classes
In traditional way, we create the instance using 'new' operator in another class. here thats instance life time is dependent on target class. its tightly coupled. Here CDI makes to loosely coupled and change the life-cycle for that instance.
CDI provides
- Life-cycle context to objects
- type safe injection
- Interceptor and event-notification
Dependency Injection is one of the great feature in Spring. but there used String based DI. here finding problems (i.e string misspelling, Incompatible classes) are in runtime only. In CDI uses annotation its find most of the problem in compile time.
Traditional Way
In ClassB class we create ClassA instance using 'new' operator and life-cycle of ClassA is bounded. But if we use CDI then instance creation responsibility goes to container (Java EE Container)
CDI Implementations
- Weld from JBoss Community
- Open WebBeans from Apache
In Weld is integrated in GlassFish server 3.0 by default. When we create project in Netbeans we need to check the Enable Context and Dependency Injection Checkbox in Project Creation wizard, Netbeans automatically create beans.xml in WEB-INF Folder . Otherwise CDI is not enabled. If u use old netbeans (older than Netbeans 6.9) then that option is not present, so u need to manualy create empty beans.xml file in WEB-INF folder. If beans.xml file is present then CDI is enabled, Otherwise its disabled.
Example
Tester.java
here we are not using any 'new' operator. Container take the responsibility to create the instance where its needed. If u inject the interface, container automatically find the implementaion of the interface and inject it.
If we have more than one implementation of particular interface then its show ambiguity exception in compile time. because container confuse which one should inject?. We solve the problem in two ways.
- We directly inject the implementation instead of interface
- We use Qualifier annotation
Please see the second part here
Screen Cast
Comments are welcomed
Please see the second part here