Monday, May 20, 2013

Contexts and Dependency Injection(CDI) - Producer


@Produces is a CDI mechanism which allows defining a source for injection. One can use Producer Method or Producer field to generate objects.


Here is brief explanation from JEE 6 tutorial:

A producer method generates an object that can then be injected. Typically, you use producer methods in the following situations:
  • When you want to inject an object that is not itself a bean
  • When the concrete type of the object to be injected may vary at runtime
  • When the object requires some custom initialization that the bean constructor does not perform
(Code snippet from attached Source)


A producer field is a simpler alternative to a producer method; it is a field of a bean that generates an object. It can be used instead of a simple getter method. Producer fields are particularly useful for declaring Java EE resources such as data sources, JMS resources, and web service references.

(Code snippet from attached Source)

This is how an Object in Injected

(Code snippet from attached Source)


This example shows both ways of declaring a producer and Injection.

Click here to download source code


Prasanna Bhale

4 comments:

  1. Nice Article. We are authorised support partner in Quickbooks Payroll. If you required any help for Quickbooks Payroll Support Phone Number 1-800-986-4607 if you are Expand your business to a new hike, with progressive approach. Seeking for the best accounting software? Then, get quickbooks installed in your system. The software proves to be more profitable to the business

    ReplyDelete
  2. We have a panel of QuickBooks Proadvisor Support Phone Number experts who can help you to solve your QuickBooks related issues. The solutions we deliver will take only few seconds of you. We have the availability 365 days a year and 24 hours a day!

    ReplyDelete
  3. I am very very impressed with your blog, I hope you will have more blogs or more articles to bring to readers. You are doing a very good job.
    bsc 1st year result 2021-22 subject wise

    ReplyDelete