CDI - Creating Factories with @Produces Annotation

CDI and the @Produces annotation for Factory


This is the Part 1 of the CDI Series in Java that contains:

  • Part 1: Factory in CDI with @Produces annotation
  • Part 2: CDI Qualifiers
  • Part 3: Events and Observers in CDI
  • Part 4: CDI and Lazy Initialization
  • Part 5: Interceptors in CDI
  • Part 6: CDI Dependency Injection and Alternatives
  • Part 7: Working with CDI Decorators

In this post we're going to see how to use @Produces annotation that is probably one of the most used annotation in the CDI World in Java!

Let's contextualize our challenge here! We must create the following steps in our program:

  • Create a Checkout class know...finish a checkout
  • Log the operations in the console
  • Create a Logger class to keep this log in its own class, keeping the cohesion
  • Customize this Logger to receive a Configuration that indicates which log mode should be used

Pretty simple, right? Let's jump into code!

Step 1 - Creating the special Main Class in CDI 2.0

Before creating the main class to execute the CDI code, let's create the important Checkout class:


In this example we're going to use CDI 2.0 that allows us to use CDI in a standalone mode!

To create a CDI Container that will bring to us the CDI power, just create the code:

Nice! Let's see the complete code to have an instance of the Checkout class:

Pretty easy, isn't? Notice the following code:

That code is the same as the following:

Now, it's time to execute our CDI example, to see that the environment!

You will see in your console something like this:

Great! Everything is up and running!

Step 2 - Creating a specialized Class for Logger

Our code should have a good cohesion, so we should create a Class that is responsible for Log some messages. This class will be called SpecialLogger, as you can see below:

Perfect! Now we'd like to update the Checkout class to use the Logger class:

Sounds good, because now the Checkout class doesn't know how a Log works under the hood, we're just using it!

Step 3 - Configuration Mode to Log Messages

Sometimes we would like to change the mode that the log is printed out. This modes could be

  • Debug Mode
  • Info Mode
  • Error Mode
  • etc

Our SpecialLogger should receive the desired Log and print out the message using this Configuration Mode.

It's time to create the Configuration class that will receive the Log mode:

I know, that could be better, but just to keep the example clean and simple 😉

Let's change the SpecialLogger class to be able to Log a message based on a Configuration mode:

As you can see, we're checking if the log should use the Debug or Info mode, otherwise the message must be printed out by using the Default mode.

Let's try to run it?

Yes, an exception will be thrown in our face. And that makes sense. CDI does not know how to inject the SpecialLogger object.

CDI will try the following steps:

  • Hey, I can see that you need an SpecialLogger object. That's ok for me. I'll try to find this class
  • I found the SpecialLogger class
  • I'll try to create a new SpecialLogger object for you right now
  • Ohhh, I can't do that! The SpecialLogger receives a LogConfiguration in its constructor. I don't know what it is and I don't know how to create this 🙁

Let's teach CDI how to use the LogConfiguration class in the next step!

Step 4 - Creating a Factory with @Produces annotation in CDI

It's time to create a Factory to create the SpecialLogger based on the LogConfiguration:

It's a pretty simple Factory class, that has the method createLogger() that returns a LogConfiguration object.

But CDI doesn't know about this class yet, or even its method! Let's use the annotation @Produces.

@Produces annotation indicates to CDI that the method createLogger() should be used when a LogConfiguration object is needed

The @Produces annotation will say to CDI:

Hey CDI, please use the createLogger() method when you need to Inject an SpecialLogger object in some place

Of course, the returned object of the method createLogger() must be the same type that you're interested in, in this case the SpecialLogger.

That's it! I really hope that this article could be helpful to you!

Follow us and always keep up to date : )


Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
CDI - Factories with @Produces Annotation - Hacking Code Java Tutorial

CDI - Factories with @Produces Annotation

Do you want to create powerful and easy Factories in CDI? Let's see how!

CDI Qualifiers Tutorial - Java CDI Guide by Hacking Code

CDI Qualifiers

Working with CDI Qualifiers
CDI Observers and Events Tutorial - Java CDI Guide by Live Coding

CDI Events and Observers

Let's see why and how to use CDI Events and Observers
CDI Lazy Initialization Tutorial - Java CDI Guide by Live Coding

CDI Lazy Initialization Tutorial

Did you know that we can initialize CDI beans lazily?
CDI Interceptors Tutorial - Java CDI Guide by Live Coding

CDI Interceptors

How to use CDI Interceptors from scratch
CDI Alternatives Tutorial - Java CDI Guide by Live Coding

CDI Alternatives Tutorial

Lear how to use Argument Matcher in its Fundamental way

CDI Decorator Tutorial - Java CDI Guide by Live Coding

CDI Decorator Tutorial

CDI Decorator is a powerful feature to deal with business Decoration!



  1. CDI – Decorators | Hacking Code - […] Part 1: Factory in CDI with @Produces annotation […]
  2. CDI – Lazy Initialization | Hacking Code - […] Part 1: Factory in CDI with @Produces annotation […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: