Dependency in Java EE--dependency lookup

Source: Internet
Author: User
Tags class definition

about dependency Management The first strategy we are going to introduce is to rely on lookup (Dependency Lookup). This strategy isjava EEin the traditional form of dependency management, where you can seejava EEin the specificationJNDI(Java naming Anddirectoryinterface) of the figure. From the name you can see that the solution to this dependency is by looking in the way.

as mentioned in the previous article, the resources to be referenced are through a pair ofnamewith theTargetThe corresponding annotation is identified. nameused to indicate the name of this resource when it is dependent, so it needs to be named when the resource annotation is placed above the class definition. But if a resource annotation is placed in a field or aSettermethod, you will not need to name it. In general, when a dependent lookup is used, the annotation is placed on the class and the name is explicitly specified. Place annotations in a field orSettermethods on how to use the next blog to discuss again.

Specifynameis useful for facilitating the caller's dynamic parsing of references. BecauseJNDIis ajava EEthe specification so all thejava EEThe application Server will supportJNDIAnd has its own local scope for each component.JNDIThe naming context, known as the environment naming contexts. When passingJNDIwhen you look for a resource, you find it in its environment naming context based on a pre-defined name. The following code masks how to use dependency lookup as aEJBcomponent for service.

@Stateless @ejb (name= "Audit", Beaninterface=auditservice.class) public class Departmentservicebean implements Departmentservice {    private auditservice audit;        @PostConstruct public    void init () {        try {            Context ctx = new InitialContext ();            Audit = (auditservice) ctx.lookup ("Java:comp/env/audit");        } catch (Namingexception e) {            throw new ejbexception (e);        }    }    public void Performaudit () {        audit.audit ();    }        // ...}

can seeDeptservicebeanis a conversationBean. It uses@EJBthe annotation declares a sessionBeanand name it "Audit". @EJBof the annotationsBeaninterfaceelement references this session.Beanthe business interfaces required in theAuditservice). In theInitmethod to find and get the appropriate resources, whereContextand theIitialcontextInterfaces areJNDIdefined by the. Contextinterface ofLookup() method is used to get theJNDIthe primary method by which the context retrieves an object. In order to find the name "Audit", the application will look for the name"Java:comp/env/audit", and convert the results toAuditserviceThe business interface. Prefix added to reference name "java:comp/env"indicates that the server should search for and discover references using environment naming and contextual questions. If the wrong name is set, an exception will be thrown when the query fails.

JNDI This approach to solving the dependency problem is universal, as long as it meets java EE standard then you can use this way to find the interface you care about (in fact, the final work is to achieve) but this method of finding resources is a bit cumbersome, similar to the home on fire and then to buy fire equipment (why not bought in advance?) This is what we want to say in our next blog post).

In addition to the above code, useContextinterface ofLookupmethod,EJBYou can also useEjbcontextinterface (and its sub-interfaces).Lookupmethod. This allows you to access the services (clocking service) while the program is running. The following code demonstrates this wayLookupmethod.

@Stateless @ejb (name= "audit310", Beaninterface=auditservice310.class) public class DepartmentServiceBean310 Implements DepartmentService310 {    //use of resource dependency injection are covered later in the chapter    @Resourc e sessioncontext context;    AuditService310 Audit;    public void Setsessioncontext (Sessioncontext context) {        This.context = context;    }        @PostConstruct public    void init () {        audit = (AuditService310) context.lookup ("audit310");    }    public void Performaudit () {        audit.audit ();    }        // ...}

Although many people on the internet say that compared Jndiapi , Ejbcontext Lookup () method has advantages. The first method parameter is exactly the name specified in the resource reference, without having to write the strange string in the first part of the code. Next is the handling of exceptions, and it is obvious that the second way the exception has not been handled manually, because the second method throws a run-time exception directly. In fact, the inside of the container will be like the first method to invoke the JNDI API to find, but it is easy for us to deal with the exception, which leads to the second way seems to not handle the above. In the author's view these two ways are not the most suitable for our way, as the above example, on fire to want to buy fire equipment, why not bought in advance? This is another strategy to address dependency-dependency injection, which we'll cover in our next blog post.

Dependency in Java EE--dependency lookup

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.