Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3948810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:25:08+00:00 2026-05-20T01:25:08+00:00

Suppose that I have two classes, first a class without any properties, fields or

  • 0

Suppose that I have two classes, first a class without any properties, fields or annotations:

public class B {}

And a class which gets B injected, like this:

public class A {
    @Inject
    private B b;

    public B getB() {
        return b;
    }
}

Now class A is pretty useless until we use it, so there are two options:

  • @Inject it
  • Construct it manually, using the trusty “new A()”

If A gets injected, CDI manages it and is kind enough to inject B which has the implicit scope of @Dependent. Cool, just what I want.

However, if I manually construct A (let’s say in a factory or a builder), CDI completely ignores my object and won’t inject an object of type B.

Example I’m talking about when it doesn’t work, here object a will always remain null:

public class Builder {
    @Inject
    private A a;

    public static Builder ofTypeSomething() {
        // do some magic here
        return new Builder();
    }

    private Builder() {
        // and some more here
    }
}

Why doesn’t this work?

Class A is a valid managed bean and has a valid scope, just like class B. Even if I add @Producer to the static method, it won’t change anything (which is fine, cause the idea of the static method is to call it, not to inject Builder anywhere).

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:25:09+00:00Added an answer on May 20, 2026 at 1:25 am

    Dependency injection, while useful, is not magical. The way DI works is that when you ask the container for an instance of an object the container first constructs it (via new()) and then sets the dependencies (how this happens depends on your framework).

    If you construct the entity yourself then the container has no idea you’ve constructed the entity and can’t set the dependencies of the entity.

    If you want to use a factory then most frameworks have some way of configuring the entity so that the container knows to make a static factory method call and not call the constructor of the entity. However, you still have to obtain your entity from the container.

    Edit: This site seems to demonstrate how to use a factory in CDI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes defined the following way: public class A { public
Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have two classes: public class Student { public int Id {get; set;}
Suppose we have declared these two classes: public class Animal { //..... } public
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have two columns in a table that represents a graph, the first
Suppose you have a Django view that has two functions: The first function renders
Suppose I have two classes. In the first one I declare this in Class1.h
Suppose I have two classes a base class and an inherited class as follows:
suppose I have two classes class A extends class B class A has its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.