Replies: 6 comments 3 replies
-
So I'll say no but it depends on whether a custom scope is appropriate. Background: Prototype scope is not Spring specific but a scope available in many DI libraries like Guice etc. An alternative to using prototype scope is of course just create the instance ourselves in application code (via using new or factory methods). As I see it, the reason why we might want prototype scope is because we want the DI library to create that particular 'bean' rather than create it ourselves. The issue here is that I've never desired this myself in all my time using Spring, Guice over many years. The reason for that is that - it makes no sense to wire a prototype scoped bean into a bean of any other scope and the alternative to prototype scope is just plain application code (new or factory method). That is, we can really only inject a prototype scope bean into other beans that are also prototype scope. We can inject beans of other scopes into prototype scope beans but this generally is better done using a simple factory method instead - we don't need DI for this. So the question becomes, when is it desirable to have DI prototype scope rather than just using just using Custom scopeWith Can you explain your use case more? I'd like to know why there could be a preference for prototype scope (vs just using new or factory method). Thanks, Rob. |
Beta Was this translation helpful? Give feedback.
-
Thanks for this great response and your precious time! |
Beta Was this translation helpful? Give feedback.
-
Right. The questions are: At the moment my thinking is:
It might be a case of doing an investigation to see what complexity it would add and more taking the view that people won't be too confused and use it appropriately. |
Beta Was this translation helpful? Give feedback.
-
FYI: I added support for Adding support didn't add much complexity at all so hopefully people don't get too confused by it, we will find out in time :) Cheers, Rob. |
Beta Was this translation helpful? Give feedback.
-
I'm going to add a second use case for That is, "Builders" by their nature most often want to be prototype scope. For example, let's say we want to use something like a FooBuilder, where some of the builder configuration is done in DI and some is done in the code/class that is going to use that builder. In this case we might have a @Factory
MyFactory {
@Prototype
@Bean
FooBuilder fooBuilder() {
... // returning a "builder of thing" rather than "thing"
}
}
Actually, code using the builder can just get the builder injected. Each time builder is injected it's a new builder instance not shared with anything else. MyHttpClientUser {
final FooBuilder builder; // this instance is not shared
MyHttpClientUser(FooBuilder builder) {
this.builder = builder;
}
void doStuff() {
..
builder.set(...) // maybe do some configuration on the builder
Foo foo = builder.build(); // gets its own unique instance of foo
...
}
} We could also inject a MyHttpClientUser {
final Provider<FooBuilder> provideBuilder;
MyHttpClientUser(Provider<FooBuilder> provideBuilder) {
this.provideBuilder = provideBuilder;
}
void doStuff() {
..
FooBuilder builder = provideBuilder.get(); // builder is not a shared instance
builder.set(...) // do some configuration on the builder maybe
Foo foo = builder.build(); // gets its own unique instance of foo
...
}
} |
Beta Was this translation helpful? Give feedback.
-
Kinda scratching my head with this one... I've never used DI before since I didn't like its "no arg constructors" ick and such.. when I heard of Avaje-inject, I jumped at the idea it could be useful for my "enforcement" constructor ideology. Alas, where is the ability to create prototype beans with arguments? TLDR; like Spring's BeanFactory#getBean(String name, Object... args) The simple use case is I'm implementing an abstract class from a thirdparty lib that has final variables set by calling super(), but would like some other fields of "mine" to be injected. more discussions: stackoverflow.com spring-bean-with-runtime-constructor-arguments |
Beta Was this translation helpful? Give feedback.
-
Hi avaje teams
First of all thanks for this very compact and powerful framework !
i've a little question , i've played recently with avaje inject,
and ask my self if there is an idiomatic way to get a spring like prototype scope or to craft it
Regards
marc
Beta Was this translation helpful? Give feedback.
All reactions