Multiple controllers with shared resources

Source: Internet
Author: User


This entry is part 3 of 15 in the series Building
Jeeapplications in javafx 2.0

In this post we're re going tocreate two controllers that share a common resource. (In this article, we will create two controllers that share common resources.) We will start with something simple
Andunimpressive in this post and refine it into something more useful insubsequent posts. (In this article, we will begin to do some simple and unimportant things, and extract it into more useful things)

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 :( currently we will create two controllers, one allowing us to edit the person object, and the other providing friendly greeting information for this person. This is what it looks like)

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.
(To make things simple, I will place two views on the same top-level page, and we will not use attribute binding, so you have to click "save" before clicking "show welcome".)
Goal here is to simply show how twocontrollers can share resources-we'll create a more specifically turally elegantsolution soon enough. (The purpose here is to simply show how to share resources between two controllers. We will create more structured solutions as soon as possible)

Our code now consists of twocontrollers, enterdetailscontroller and welcomecontroller,
And these each have a corresponding fxmlfile, enterdetails. fxml and welcome. fxml.
(Our code is composed of two controllers: enterdetailscontroller and welcomecontroller. Both of them have the same fxml file: enterdetails. fxml and welcome. fxml) These
Are pretty unexciting and straightforward-you can check out the Code directly for more details.
(This is not interesting and simple. You can directly view the code to get more information) the only interesting thing in ourcontrollers is that they both define the following :( the only interesting thing is that the following statements are defined in our controllers)

@AutowiredprivatePerson person;

This tells spring that we wantthe person to be injected (aka 'wiredup') automatically. (This tells spring that we want automatic person injection)

In our factory, we now load eachof the controllers in separate methods in the factory and annotate thesemethods with @ bean.
(In our factory, we load the respective controllers in two different methods and use @ bean for annotation) spring will then injectSame person
Instance
Into each. (spring will inject the same person object to their respective factories)
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. (although we have created a new person object in the person () Factory method, because @ bean annotation spring knows that the cached result is reused for all injections)

Our factory now looks like this :( our factory now looks like this)


@ConfigurationpublicclassSampleAppFactory{    @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! (This is the best way to inject shared resources through dependency, how simple it is. This should be the full form of dependency injection)

Of course, our example is stillpretty raw and clunky but we'll work on this over the next few posts to cleanit all up. (Of course, our example code is still redundant, but we will clean it up in the following articles)

The full source code for this post can befound at: http://code.google.com/p/jfxee/source/browse/trunk/jfxspring3/

Posted in uncategorized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.