Seam Learning (6)-bidirectional Injection & factory and Manager Components

Source: Internet
Author: User
Bidirectional Injection

Dependency injection (dependency injection)AndInversion of Control)Currently, most Java developers are familiar with the concept. Dependency injection allows one component to inject another component to a setter method or instance variable through the container to obtain reference of the injected component ). All the dependency injection implementations we have seen before. Injection occurs when a component is created and will not change throughout the lifecycle of the instance. This makes sense for stateless components. From the client perspective, all instances of a specific type of stateless component can be replaced. On the other hand, seam focuses on processing stateful components. In this case, the traditional dependency injection is no longer very effective. Seam introducedBijection (bidirectional injection)This term is used as a generalized generalization of injection. Compared with injection (unidirectional injection), Bijection is:

    • Contextual (context-related)-Bidirectional injection is used to assemble stateful components for different contexts (components in a large range of contexts can Reference Components in a small scope)

    • Bidirectional (bidirectional)-After being triggered, the value is injected from the context variable to the component property, or from the component property.Outjected (reverse injection)Back to the context, so that the called component can operate the value of the context variable only by rewriting its instance variable.

    • Dynamic (dynamic)-Because the value of the context variable changes over time and the seam component is stateful, bidirectional Injection occurs every time the component is called.

Basically, by setting whether the instance variables require injection, reverse injection, or both, bidirectional injection allows you to map context variables to component instance variables. Of course, we use annotations to set bidirectional injection.

@ InThe annotation specifies the injection value, which may be the injection of instance variables:

@ Name ("loginaction") @ statelesspublic class loginaction implements login {@ in user ;...}

Or inject the setter method:

 
@ Name ("loginaction") @ statelesspublic class loginaction implements login {user; @ in public void setuser (User user) {This. User = user ;}...}

By default, seam searches all contexts for the injected attributes or instance variable names. If you want to explicitly specify the context variable name, you can write as follows:@ In ("currentuser ").

If no component instance is bound to a named context variable, you may want to create a seam instance. You can specify@ In (create = true). If the value is optional (can be null), specify@ In (required = false).

For some components, specify@ In (create = true)It is very tedious. You can annotate the entire component@ AutocreateIt will be automatically created as needed, without explicitly specifyingCreate = true.

You can also inject the value of the expression:

 
@ Name ("loginaction") @ statelesspublic class loginaction implements login {@ in ("# {user. Username}") string username ;...}

(In the next chapter, there will be more information about the component lifecycle and injection .)

@ OutThe annotation specifies that an attribute needs to be injected externally, possibly from the instance variable:

 
@ Name ("loginaction") @ statelesspublic class loginaction implements login {@ out user ;...}

Or use the getter method:

 
@ Name ("loginaction") @ stateless @ interceptors (seaminterceptor. class) public class loginaction implements login {user; @ out public user getuser () {return user ;}...}

Attributes can be injected or injected externally:

 
@ Name ("loginaction") @ statelesspublic class loginaction implements login {@ in @ out user ;...}

Or:

 
@ Name ("loginaction") @ statelesspublic class loginaction implements login {user; @ in public void setuser (User user) {This. user = user ;}@ out public user getuser () {return user ;}...}
Bytes ---------------------------------------------------------------------------------------------------------------------------
Factory and Manager Components

We often need to deal with objects of non-seam components. But we still want to pass them through@ InInject our components and use them in values and method expressions. Sometimes, we even need to bind them to the lifecycle of the seam context (for example@ Destroy). Therefore, seam context can accommodate non-seam component objects, and seam provides some good features, which make it easier to deal with non-Component Objects bound to the context.

Factory component pattern (factory Component Mode)Make the seam component a constructor for non-component objects. When the context variable is referenced but no value is bound to it,Factory method). We pass@ FactoryAnnotation to define the factory method. The factory method binds a value to the preceding context variable and determines the scope of the bound value. There are two factory methods. First, return a value. seam binds it to the context:

 
@ Factory (scope = conversation) public list <customer> getcustomerlist () {return ...;}

Method 2VoidTo bind the value to the context variable:

 
@ Datamodel list <customer> customerlist; @ Factory ("customerlist") Public void initcustomerlist () {customerlist = ...;}

In either case, when we referenceCustomerlistWhen the value of context variable is null, the factory method is called, and other parts of the life cycle of this value cannot be manipulated. The more powerful mode isManager Component pattern (Manager Component Mode). In this case, a seam component is bound to a context variable, which manages the value of the context variable and is invisible to the client.

The Manager component can be any component, and it needs@ UnwrapMethod. This method returns the value visible to the client,Each timeContext variables are called when referenced.

 
@ Name ("customerlist") @ scope (conversation) public class customerlistmanager {... @ unwrap public list <customer> getcustomerlist () {return ...;}}

When you have an object and need more control over its component lifecycle, the component management mode is particularly useful. For example, if you have a heavyweight Object that you want to clear when the context ends, you can@ UnwrapObject, and@ DestroyMethod.

@ Name ("hens") @ scope (Application) public class henhouse {set <strong> hens; @ in (required = false) handle tables; @ unwrap public list <strong> gethens () {If (hens = NULL) {// setup our hens} return hens;} @ observer ({"chickborn ", "chickenboughtatmarket"}) Public addhen () {hens. add (Actions) ;}@ observer ("chickensoldatmarket") Public removeapps() {hens. remove (plaintext) ;}@ observer ("foxgetsin") Public removeallhens () {hens. clear ();}...}

Here, the managed components observe the events that change the objects below. The component manages these actions by itself and provides a unified view because the object is unlocked during each access.

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.