09.04.2018 Views

tornadofx-guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dependency Injection<br />

val MyView : View() {<br />

}<br />

val helloService: HelloService by di()<br />

The di delegate accepts any bean type, while inject will only allow beans of type<br />

Injectable , which includes TornadoFX's View and Controller . This keeps a clean<br />

separation between your UI beans and any beans configured in the external dependency<br />

injection framework.<br />

Setting up for Spring<br />

Above the setup for Guice is shown. Setting up for Spring, in this case using beans.xml as<br />

ApplicationContext is done as follows:<br />

beans.xml<br />

<br />

<br />

<br />

<br />

<br />

This sets Spring up to scan for beans.<br />

Application startup<br />

class SpringExampleApp : App(SpringExampleView::class) {<br />

init {<br />

val springContext = ClassPathXmlApplicationContext("beans.xml")<br />

FX.dicontainer = object : DIContainer {<br />

override fun getInstance(type: KClass): T = springContext.get<br />

Bean(type.java)<br />

}<br />

}<br />

}<br />

266

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!