Java context and Dependency injection (JSR 299) [1] __java

Source: Internet
Author: User
Tags generator
Java Context and Dependency injection (JSR 299) [1]Reprint please retain the author information:
Author: 88250
blog:http:/blog.csdn.net/dl88250
MSN & Gmail & qq:dl88250@gmail.com

Summary
Starting with the JSR 299 specification, this paper collates and translates the very important concepts in the specification and describes the use of JSR 299 in terms of short examples. The article briefly describes how to use the XML configuration bean, which is mainly about using callout (annotation) configuration.
1. binding
1.1 Defining a new binding type
@BindingType
@Retention (RUNTIME)
@Target ({method, FIELD, PARAMETER, TYPE})
Public @interface Synchronous {}

@BindingType
@Retention (RUNTIME)
@Target ({method, FIELD, PARAMETER, TYPE})
Public @interface Asynchronous {}

Two binding types are defined here, @Synchronous and @Asynchronous, and you can use them later to define the bean, which is the binding.

For a type-safe resolution algorithm, the member values of the binding type are very important:
@BindingType
@Retention (RUNTIME)
@Target ({method, FIELD})
Public @interface Payby {
PaymentMethod value ();
}
1.2
Binding
This uses the binding type above for two beans bindings:
@Synchronous
public class Synchronouspaymentprocessor
Implements Paymentprocessor {
...
}

@Asynchronous
public class Asynchronouspaymentprocessor
Implements Paymentprocessor {
...
}

These two beans implement the Paymentprocessor bean type and can be injected as needed on the injection point:
@Synchronous Paymentprocessor Paymentprocessor;
@Asynchronous Paymentprocessor Paymentprocessor;

A bean can define multiple binding types:
@Synchronous @Reliable
public class Synchronousreliablepaymentprocessor
Implements Paymentprocessor {
...
}
1.3 Default BindingsThe default bindings can use @Current:
@Current
public class Order {}
Equivalent to
public class Order {}

At the time of injection,
public class Order {
Public order (@Current Orderprocessor processor) {...}
}
Equivalent to
public class Order {
Public order (Orderprocessor processor) {...}
}
2. Scope 2.1 Built-in Scopes 2.1.1 @sessionscoped
The scope is activated in any Servlet.service () method and spans any Filter.dofilter () method. The session context will be shared by all servlet requests within the same HTTP servlet session, destroyed at the Httpsession.invalidate () method call.
2.1.2 @RequestScoped
The scope is activated in any Servlet.service () method and spans any Filter.dofilter () method. The Request context will be destroyed at the end of the servlet request, that is, after the Sevlet.service () method and all the Filter.dofilter () methods are returned, the scope is activated in the Java EE Web service call, and the Destroying a Web service call when it is activated on any asynchronous observer method notification, and destroying the scope when the notification is finished is activated to arbitrary EJB remote method calls and spans any EJB timeout method and message delivery for any EJB message-driven bean. The context is destroyed when a remote method call completes, a time-out, or a message delivery completes2.1.3 @ApplicationScoped
The scope is activated in any Servlet.service () method and across any Filter.dofilter () method that is activated when the Java EE Web service invokes the scope that is activated to any asynchronous observer method notification. To arbitrary EJB remote method calls and across arbitrary EJB timeout methods and message delivery for any EJB message-driven bean
The application context is shared by all servlets, asynchronous observer method notifications, Web service calls, EJB remote method calls, EJB timeout methods, and message delivery execution for EJB message-driven beans in the same application and is destroyed when the application is deployed. This scope can be viewed as a static scope within an application.

2.1.4 @ConversationScoped
Follow-up article analysis:-)

2.1.5 @Dependent
Follow-up article analysis:-)

The scope type is extensible:
@ScopeType
@Inherited
@Target ({TYPE, method, FIELD})
@Retention (RUNTIME)
Public @interface businessprocessscoped {}

3. Deployment Type
3.1 Type of deployment built in
Built-in deployment type: @Standard, @Production

The deployment type is extensible:
@DeploymentType
@Target ({TYPE, method, FIELD})
@Retention (RUNTIME)
Public @interface Australian {}
****
@Production
public class Taxpolicies {
@Produces @Australian
Public Taxpolicy Getaustraliantaxpolicy () {...}
}

By default, if the specified deployment type callout is not displayed, the Generator method or field in the bean inherits the type of deployment of the bean. If the bean display declares a deployment type, the deployment type declared by the configuration is ignored.
4. Configuration
In the architecture design, configuration is a very important concept. A configuration defines a bean role that allows developers to uniformly identify common metadata for such beans. In JSR 299, a configuration can encapsulate any combination of the following concepts:
A default deployment type a limit of a default scope to a bean scope (restriction) a collection of interceptor bindings for either the implementation of a bean or the need for an extension of a type
4.1 Defining a new configuration
@Stereotype
@RequestScoped
@Production
@Target (TYPE)
@Retention (RUNTIME)
Public @interface Action {}

Beans with @Action annotations will default to the @RequestScoped scope and @Production deployment type unless the other scope and deployment type is displayed on the bean:

@Mock @ApplicationScoped @Action
public class Mockloginaction extends Loginaction {...}

4.2 Built-in configurations
There are three types of built-in configurations:
@Model
@Interceptor
@Decorator


PostScript

The JSR 299 specification is still in the draft phase, and it has changed a lot since EDR (Early Draft Review):
Canonical name from Webbeans-> Java contexts and Dependency inject-> contexts and Dependency for Java injection current Community drafts C Ontexts and Dependency injection for Java EE from explicit support for Spring Bean & Seam Components-> support from SE support using SPIs method-> No support (Webbeans RI is supported)-> does not provide XML configuration from providing XML configuration http://in.relation.to/Bloggers/NewDraftOfJSR299ForReviewByTheCommunity
However, its core idea is that the server-side component State model has not changed at all, and the integration of JSF EJB has not changed. Of course, the current change is not very stable, the final draft may be released next month, wait and see.
terminology-English-Chinese comparison Annotation: Callout
Binding (Type): Binding (types)
Scope: Scope
Asynchronous: Asynchronous
Observer: Observer
Context: Contexts
Deployment (Type): Deployment (Types)
Stereotype: Configuration
Producer (method): Generator (methods)
Field: Fields
Injection point: Injection points
Bean Type:bean Type

-->

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.