Dependency Injection and AOP Brief (11)-Lifecycle management.

Source: Internet
Author: User

2. Life Cycle Management

The various dependency injection frameworks provide a convenient function for developers to manage various scopes, which inevitably comes with the managed lifecycle management of dependent objects. The so-called life cycle management, is an object in the scope to which it belongs from the beginning of the container creation, to be provided to the dependent, and then to the final extinction of the entire process, the dependency Injection framework provides a series of callback methods of the interface, Both the framework itself and the developers can use these interfaces to do some operations and management of the dependent objects at each time of survival.

For example, a dependency injection container, when creating a dependent object, is far less simple than the new object, but rather a process that is extremely complex to wrap the object. That spring, creating an object in the owning scope is going through a large and small 9 stages (the Xxxaware interface that spring developers often use in this case). The seam framework provides a more flexible and concise way to illustrate the seam framework's lifecycle management in one of these ways.

Suppose for the bank this dependent object, when its opening, that starts to enter the application level scope The survival period time, must be many such as arranges the salesperson, the employ security and so on initialization action, only these initialization action completes, can make as a complete object provides to the dependent person. This scenario is translated using the seam framework to write the following code:

@Name ("bank")

@Scope (Scopetype. Application)

Public class BANKICBC implements Bank {

@Create//Labeling as Initialization life cycle method

Public void Beforeopenbank () {

Assignemployee ();

Hiresecurityguard ();

......

}

@Override

Public Cash Withdraw (Depositbook depositbook, BigDecimal amount) {

......

}

}

You can see that the seam framework provides @create annotations to indicate an initialized life-cycle callback method that allows the container to call the callback method labeled @create before it creates a "bank" dependency, and after the method returns successfully, will be managed in a dependency injection container ready to be provided to the relying person. Similarly, when an object is about to go out of scope, it often requires some work before the object is destroyed, and seam provides a @destroy annotation to mark a destroyed callback method.

Dependency Injection and AOP Brief (11)-Lifecycle management.

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.