Java context and dependency injection (JSR 299) [1]

Source: Internet
Author: User
Document directory
  • 1.3 default binding
  • 2.1 built-in Scope
Java context and dependency injection (JSR 299) [1]

Reprinted by the author:
By: 88250
Blog: http:/blog.csdn.net/dl88250
MSN & Gmail & QQ: DL88250@gmail.com

Summary

This article starts with the JSR 299 specification, sorts out and translates very important concepts in this specification, and describes the use of JSR 299 in combination with some short examples. This article describes how to configure bean with XML, and mainly introduces how to configure bean with annotation.
1. Bind
1.1 define 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 {}

TwoBinding type, @ Synchronous and @ asynchronous. You can use them to define beans later.Bind.

For the type Security Resolution Algorithm,Binding typeIs very important:
@ Bindingtype
@ Retention (runtime)
@ Target ({method, field })
Public @ interface payby {
Paymentmethod value ();
}
1.2 bind

Use the precedingBinding typeForBind:
@ Synchronous
Public class synchronouspaymentprocessor
Implements paymentprocessor {
...
}

@ Asynchronous
Public class asynchronouspaymentprocessor
Implements paymentprocessor {
...
}

Both beans implement the paymentprocessorBean type, InInjection PointInjection can be performed as needed:
@ Synchronous paymentprocessor;
@ Asynchronous paymentprocessor;

One bean can define multipleBinding type:
@ Synchronous @ reliable
Public class synchronousreliablepaymentprocessor
Implements paymentprocessor {
...
}
1.3 default binding

Default bindingYou can use @ current:
@ Current
Public class order {}
Equivalent
Public class order {}

During injection,
Public class order {
Public Order (@ current orderprocessor processor ){...}
}
Equivalent
Public class order {
Public Order (orderprocessor processor ){...}
} 2. Scope 2.1 built-in scope 2.1.1 @ sessionscoped

This scope is activated in any servlet. Service () method and spans any filter. dofilter () method. SessionContextAll servlet requests in the same HTTP servlet session will be shared and destroyed when the httpsession. invalidate () method is called.
2.1.2 @ requestscoped

  • This scope is activated in any servlet. Service () method and spans any filter. dofilter () method. RequestContextWill be destroyed at the end of the Servlet request, that is, after the sevlet. Service () method and all filter. dofilter () Methods return
  • This scope is activated for Java EE web service calls and destroyed when the web service call is completed.
  • This scope is activated on anyAsynchronous observerMethod notification and destroyed when the notification is completed
  • This scope is activated for any remote EJB method call, and spans any EJB timeout method and message delivery for any EJB message-driven bean. The context is destroyed when the remote method call is completed, timeout, or message delivery is complete.
2.1.3 @ applicationscoped
  • This scope is activated in any servlet. Service () method and spans any filter. dofilter () method.
  • This scope is activated for Java EE web service calls
  • This scope is activated on anyAsynchronous observerMethod notification
  • This scope is activated for any remote EJB method call, and spans any EJB timeout method and message delivery for any EJB message-driven bean.

ApplicationContextAll Servlets,Asynchronous observerMethod notification, Web Service call, EJB remote method call, EJB timeout method, and message delivery execution of the EJB message-driven bean are shared and destroyed during anti-deployment of the application. This scope can be viewed as a static scope in the application.

2.1.4 @ conversationscoped

Subsequent article analysis

2.1.5 @ dependent

Subsequent article analysis

The scope type is extensible:
@ Scopetype
@ Inherited
@ Target ({type, method, field })
@ Retention (runtime)
Public @ interface businessprocessscoped {}

3. Deployment type
3.1 Built-in deployment types

Built-inDeployment type: @ Standard, @ Production

Deployment typeIs scalable:
@ Deploymenttype
@ Target ({type, method, field })
@ Retention (runtime)
Public @ interface extends Alian {}
****
@ Production
Public class taxpolicies {
@ Produces @ Australian
Public taxpolicy getaskaliantaxpolicy (){...}
}

By default, if the specified deployment type annotation is not displayed, the generator method or field in the bean inherits the deployment type of the bean. If the bean shows that the deployment type is declared, the deployment type declared by the configuration will be ignored.
4. Configuration

In architecture design, architecture is a very important concept. A configuration defines a bean role that allows developers to identify common metadata for such beans. In JSR 299, the configuration can encapsulate any combination of the following concepts:

  • One default deployment type
  • A default Scope
  • Restriction)
  • Requirement for one bean implementation or one type Extension
  • A collection bound to an interceptor

4.1 define a new configuration

@ Stereotype
@ Requestscoped
@ Production
@ Target (type)
@ Retention (runtime)
Public @ interface action {}

All beans marked with @ action will have the scope of @ requestscoped and the deployment type of @ production by default, unless other scopes and deployment types are displayed on the bean:

@ Mock @ applicationscoped @ action
Public class mockloginaction extends loginaction {...}

4.2 built-in Configuration

There are three built-in configurations:
@ Model
@ Interceptor
@ Decorator

Postscript

The JSR 299 specification is still in the draft phase and has changed significantly since the publication of the EDR (early draft review:

  1. Standard name from webbeans-> JAVA contexts and dependency inject-> contexts and dependency injection for Java-> currently the latest Community draft contexts and dependency injection for Java EE
  2. Explicit support for spring bean & seam components-> spis support
  3. From se support-> not supported (webbeans Ri is supported)
  4. From providing xml configuration-> not providing xml configuration
  5. Http://in.relation.to/Bloggers/NewDraftOfJSR299ForReviewByTheCommunity

However, its core idea is that the state model of the server component remains unchanged, and the integration of jsf ejb remains unchanged. Of course, the current specification change is not very stable, and the final draft may be released in the next month.
Terms

Annotation: Annotation
Binding (type): binding (type)
Scope: Scope
Asynchronous: asynchronous
Observer: Observer
Context: Context
Deployment (type): Deployment (type)
Stereotype: Configuration
Producer (method): generator (method)
Field: Field
Injection point: Injection Point
Bean type: bean type

Related Article

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.