-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Lazy initialization #516
Comments
Inject is already pretty fast for wiring compared to other DI frameworks, for what purpose do you need lazy beans? |
I am writing a desktop application using javafx, I have some UI component bean for example: click a button and popup a dialog and the dialog is a bean. |
Tried using the Provider Interface? you can wire a bean provider for cases where you don't want to immediately instantiate. |
Yeah, I had tried, I think this will make a lot of template code in my application cause there are many UI component need as a bean, if the DI framework support lazy initialization option that will be great. |
you could make a utility method to convert a supplier to a cached value. (btw you can also use public static <T> Supplier<T> memoize(Supplier<T> original) {
return new Supplier<T>() {
Supplier<T> delegate = this::firstTime;
boolean initialized;
public T get() {
return delegate.get();
}
private synchronized T firstTime() {
if (!initialized) {
T value = original.get();
delegate = () -> value;
initialized = true;
}
return delegate.get();
}
};
} Then you can do: @Singleton
class MyFXService {
Supplier<MyObject> supplier;
MyFXService(Supplier<MyObject> supplier) {
this.supplier = memoize(supplier);
}
void someMethod() {
var value = supplier.get();
// idk do something
}
} |
@DaiYuANg can you show us what your code looks like? I don't know much about JavaFX (nor swing) these days. So the idea of using DI to inject a dialog is something I can't easily visualise in terms of what that code actually looks like. To be clear I believe we are talking about "Lazy Singleton" (only 1 instance initialised lazily on demand - so a Now currently we treat a So as a following thought, is that maybe we could have a |
Yeah, I tried the Supplier way |
Of course, for example: @Factory
public class RootFactory {
@Bean
PreferencesFx preferencesFx() {
return PreferencesFx.of(
SaveClass.class,
Category.of(
"Category Title",
Group.of("Group Title", Setting.of("Setting Title", new SimpleStringProperty()))));
}
@Bean
Preferences preferences() {
return Preferences.systemRoot();
}
} And then there is a controller @Singleton
@Slf4j
public class GlobalMenuBarController implements Initializable {
//this is click event handle
public void openPreferences(ActionEvent actionEvent) {
//DIContext is a BeanScope wrapper
val preferencesFx = DIContext.get(PreferencesFx.class);
preferencesFx.show(true);
}
} In my case, maybe |
try adding |
Yes, @Lazy @Bean
PreferencesFx preferencesFx() {
... Given this is used via So yes, I think we can add some proper support for this. |
@DaiYuANg just saying, 9.12-RC1 is in maven central so you can give this a try |
Hey, thanks so much 😸 |
How to make Bean is lazy, Maybe we can be using Supplier and at runtime Supplier.get() to inject instance
The text was updated successfully, but these errors were encountered: