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.