This entry is part 3 of 15 in the series Building JEEapplications in JavaFX 2.0
In this post we’re going tocreate two controllers that share a common resource.(在這篇文章中我們将要建立兩個控制器,它們共享共同的資源) We will start with something simple andunimpressive in this post and refine it into something more useful insubsequent posts.(我們在這篇文章将要開始做一些簡單不重要的事情,在後面的文章中把它提煉成更加有用的東西)
For now we will create twocontrollers, one that allows us to edit our Person object, and another thatprovides a friendly welcome message for that person. Here’s how it will look:(目前我們将要建立兩個控制器,一個允許我們去編輯Person對象,另一個為這個人提供了友好的問候資訊。這就是它看起來的樣子)
To keep things simple, I’ve justplaced the two views on the same top-level page and we’re not using propertybindings so you will have to hit the ‘save’ button before hitting the ‘showwelcome’ button. (為了讓事情簡單,我将會在相同的頂層頁面放置兩個視圖,我們不會用到屬性綁定,是以你不得不在點選“show welcome”按鈕之前點選“save”按鈕)The goal here is to simply show how twocontrollers can share resources – we’ll create a more architecturally elegantsolution soon enough.(這裡的目的隻是簡單的顯示如何在兩個控制器之間共享資源,我們将會盡快建立更多結構優美的解決方案)
Our code now consists of twocontrollers, EnterDetailsController and WelcomeController, and these each have a corresponding FXMLfile, EnterDetails.fxml and Welcome.fxml. (我們的代碼是由兩個控制器組成的,分别是EnterDetailsController 和WelcomeController,它們都有一緻的FXML檔案: EnterDetails.fxml 和Welcome.fxml)These are pretty unexciting and straightforward – you can check out the code directly for more details. (這個足夠令人不感興趣和簡單直接,你可以直接檢視這些代碼得到更多的資訊)The only interesting thing in ourcontrollers is that they both define the following:(唯一令人感興趣的事情是在我們的控制器它們都定義了下面的語句)
@AutowiredprivatePerson person;
This tells Spring that we wantthe Person to be injected (aka ‘wired up’) automatically.(這個告訴Spring我們想要Person自動注入)
In our factory, we now load eachof the controllers in separate methods in the factory and annotate thesemethods with @Bean. (在我們的工廠中,我們在兩個不同的方法中加載各自的控制器使用@Bean進行注解)Spring will then inject the same person instance into each.(Spring将會注入相同的person對象到各自的工廠中) Even though we create a newPerson in the person() factory method, Spring knows to cache the result andreuse it for all injection because of the @Bean annotation.(盡管我們在person()工廠方法中建立了新的Person對象,因為@Bean注解Spring知道緩存這個結果為所有的注入重用)
Our factory now looks like this:(我們的工廠現在看起來是這樣子的)
@Configuration
publicclassSampleAppFactory
{
@Bean
publicPerson person()
{
returnnewPerson();
}
@Bean
publicWelcomeController welcomePageController() throwsIOException
{
return(WelcomeController) loadController("/Welcome.fxml");
}
@Bean
publicEnterDetailsController enterDetailsController() throwsIOException
{
return(EnterDetailsController) loadController("/EnterDetails.fxml");
}
protectedObject loadController(String url) throwsIOException
{
InputStream fxmlStream = null;
try
{
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = newFXMLLoader();
loader.load(fxmlStream);
returnloader.getController();
}
finally
{
if(fxmlStream != null)
{
fxmlStream.close();
}
}
}
}
That’s it – sharing resources ina nice way via Dependency Injection is actually incredibly simple. It ought tobe, that’s really what dependency injection is all about!(這就是通過依賴注入共享資源的最好的方法,它實際上是多麼的簡單。這就應該是依賴注入的全部)
Of course, our example is stillpretty raw and clunky but we’ll work on this over the next few posts to cleanit all up.(當然,我們的例子代碼依然是備援的,但我們在接下來的幾個文章中把它清理幹淨)
The full source code for this post can befound at: http://code.google.com/p/jfxee/source/browse/trunk/jfxspring3/
Posted in Uncategorized.