Skip to content
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

Creation before injection #78

Open
goto1134 opened this issue Aug 14, 2017 · 0 comments
Open

Creation before injection #78

goto1134 opened this issue Aug 14, 2017 · 0 comments

Comments

@goto1134
Copy link

goto1134 commented Aug 14, 2017

The example can be taken from goto1134/javafx-frameworks-comparation#afterburner-failing-injection branch.

Description

My presenter class:

class AfterburnerPresenter : Initializable {
    override fun initialize(location: URL?, resources: ResourceBundle?) {
        sayHelloButton.setOnAction { helloLabel.text = helloUserServiceImpl.getHelloStringForUser(User(nameTextField.text, surnameTextField.text)) }
    }


    @Inject lateinit var helloUserServiceImpl: HelloUserServiceImpl
    @FXML lateinit var nameTextField: TextField
    @FXML lateinit var surnameTextField: TextField
    @FXML lateinit var helloLabel: Label
    @FXML lateinit var sayHelloButton: Button
}

I try to inject spring beans in a presenter, using this code:

val view: AfterburnerView = AfterburnerView({ name -> context.getBean(name) })

And works fine until I use HelloUserService interface instead of HelloUserServiceImpl. This stacktrace appears after crash:

Caused by: java.lang.IllegalStateException: Cannot instantiate view: interface com.github.goto1134.fxframeworks.HelloUserService
	at com.airhacks.afterburner.injection.Injector.lambda$getDefaultInstanceSupplier$9(Injector.java:228)
	at com.airhacks.afterburner.injection.Injector.instantiateModelOrService(Injector.java:111)
	at com.airhacks.afterburner.injection.Injector.injectMembers(Injector.java:144)
	at com.airhacks.afterburner.injection.Injector.injectMembers(Injector.java:129)
	at com.airhacks.afterburner.injection.Injector.injectAndInitialize(Injector.java:122)
	at com.airhacks.afterburner.injection.Injector.registerExistingAndInject(Injector.java:103)
	at com.airhacks.afterburner.injection.Injector.instantiatePresenter(Injector.java:56)
	at com.airhacks.afterburner.views.FXMLView.lambda$loadSynchronously$1(FXMLView.java:85)
	at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
	at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
	at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
	at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
	at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
	... 15 more
Caused by: java.lang.InstantiationException: com.github.goto1134.fxframeworks.HelloUserService
	at java.lang.Class.newInstance(Class.java:427)
	at com.airhacks.afterburner.injection.Injector.lambda$getDefaultInstanceSupplier$9(Injector.java:226)
	... 28 more
Caused by: java.lang.NoSuchMethodException: com.github.goto1134.fxframeworks.HelloUserService.<init>()
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.newInstance(Class.java:412)
	... 29 more

Problem

The problem is in the logic of initialization with its injection and postinjection steps, where on regular injection step the Injector creates an object for the class of the parameter.

My vision of solution

The situation when you create and inject object which already exists and should be injected on postinjection step is incorrect and should be fixed. Also doing injection this way may cause unpredictable behaviour in some (bad designed) applications.

Moreover, I don't see any reason to use two-step injection. If an object exists in injectionContext, it should be injected on the only injection step immediately.

Correct me if I'm wrong.

tobiasdiez added a commit to tobiasdiez/afterburner.fx that referenced this issue Oct 22, 2017
mduffin95 added a commit to mduffin95/afterburner.fx that referenced this issue Jan 14, 2019
Fix for AdamBien#71 and AdamBien#78: Use injection context already on inital presente…
dlemmermann added a commit to dlsc-software-consulting-gmbh/afterburner.fx that referenced this issue Aug 20, 2020
…ien#78: Use injection context already on inital presenter initialization AdamBien#80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant