Using @Startup with @Produces methods #49069
-
Creating an However, I want to create instances of my bean via a producer method such that I get the same behaviour:
So what I have is: public class TestBean {
public TestBean() {
System.out.println("constructed");
}
@PostConstruct
void init() {
System.out.println("I'M ALIVE!");
}
}
public class Producers {
@Produces
@Startup
TestBean testBean() {
System.out.println("constructing");
return new TestBean();
}
} I see messages:
but the Am I misunderstanding how the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Beta Was this translation helpful? Give feedback.
-
Note that your bean does not have a scope and will therefore be |
Beta Was this translation helpful? Give feedback.
I don't think you have the right mental model.
Normal scoped beans (such as
@ApplicationScoped
or@RequestScoped
) are never injected directly. A client proxy is injected instead, which forwards invocations to the correct instance, and that instance is created lazily. So you cannot really say that "wiring is complete before startup", unless all your beans are@Singleton
or@Dependent
.(Note that this depends on what you mean by "wiring", which might be different from my understanding of the term.)
You cannot mix
@PostConstruct
callbacks and startup observers like this…