Java ee6 standard CDI tutorial Part 3

Source: Internet
Author: User


This section describes @ Alternative and @ Qualfiers
1. Use @ Alternative to select an Alternative
You can also remember that we have defined several runners that can be used as alternative selection. They are classified into JsonRestAtmTransport and SoapRestAtmTransport. If you are an ATM installer, You need to configure the transmitter and the location. The preset injection point is only for StandardRestAtmTransport.
If you still need different transmission, you need to change the/META-INF/beans. xml file to select the correct transmission, such as below:
Example 1. {classpath}/META-INF/beans. xml
<Beans xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation ="
Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/beans_1_0.xsd>
<Alternatives>
<Class> org. cdi. advocacy. JsonRestAtmTransport </class>
</Alternatives>
</Beans>


In the output, you will see that the selection uses the json rest transmitter.
Output
Deposit called
Communicating with bank via json rest transport
Alternative Selection is a common scenario in DI, that is, you have different injection objects depending on different architectures. The good thing is that the object can be replaced. The Alternative Selection feature allows you to mark the object to be replaced by its counterpart.
If the DI container has the alternative power, you can mark the object to be replaced. Consider using this method. Because we do not need to say in the document that we choose the alternative, he is a document, if some people know CDI and choose alternative alternatives, then they will not be able to see it. Alternative Selection is the standard method that allows you to substitute for the object.
You can use CDI as a standard for many models. We have already used it in many pure DI frame frames. Simplification, standardization, and standardization are the focal points of DI developers.
2. Use @ Qualifier to inject different class types
All the objects and producers in CDI are limited to fixed classes. If you do not have a configuration limit, then the @ Default and @ Any values will be used. Just like a sin in the United States, if there is not enough money for the lawyer, then he will be allocated with one.
The restricted class type is used to identify whether the correct object is injected. You can write the self-defined restricted class type.
The limited class type can be configured with the entry label and the entry source, and the correct class type is injected.
You can decide when to add the Soap, Json, and Standard runners. You don't want to list them for selection. In practice, you want to use Json to implement them at some time.
The bottom layer is a restricted class definition for Soap.
Example 2. Limited class injection during Soap operation
Package org. cdi. advocacy;

Import java. lang. annotation. Retention;
Import java. lang. annotation. Target;
Import static java. lang. annotation. ElementType .*;
Import static java. lang. annotation. RetentionPolicy .*;

Import javax. inject. Qualifier;


@ Qualifier @ Retention (RUNTIME) @ Target ({TYPE, METHOD, FIELD, PARAMETER })
Public @ interface Soap {

}


Note that a restricted class type is a runtime injection, which is marked with @ Qualifier injection. @ Qualifier Note: note the definition of a carrier as a restricted class.
Next I use a new restricted class type @ Soap at soapatm transport:
Example 3. Use the new @ Soap restricted class injection method for soapatm transport
Package org. cdi. advocacy;

@ Soap
Public class soapatm transport implements ATM transport {

@ Override
Public void communicateWithBank (byte [] datapacket ){
System. out. println ("communicating with bank via Soap trans

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.