Dependency in Java EE--declaring dependencies

Source: Internet
Author: User

has been under The summary describes some java EE and the EJB resource annotations defined by the specification. Each annotation has an optional name attribute to specify the reference name of the dependency. The other attributes of the annotation are specific to the type of resource you want to get.

reference persistence context

persistence entitymanagerfactory javaee environment, @ Persistencecontext Annotations can be used to declare a dependency on a persisted context. And you can automatically get the entity manager that is required for that persistence context.

The following code demonstrates how to use the @PersistenceContext annotations to obtain an entity manager through dependency injection. the unitname element specifies the name of the persistence unit on which the persistence context is based.

Tip: If you ignore Unitname element, then how to determine the name of the cell that is used for the persistence context is vendor-specific. If the application has only one persistence unit, some vendors can provide default values, while other vendors may require that the cell name be specified in a vendor-specific profile.

Code

when accepted in a stateless sessionBeanyou may want to know how to ensure that the code is valid after you use the warning in the Status field. After all, the entity manager must maintain its own state in order to be able to manage a specific persistence context. The good news isJPAThe specification is designed tojava EEintegration for the purpose, so unlike the previous chapters, the code was injected in the Middle Ages not an entity manager instance. InjectedBeanvalue is a container-managed proxy that will represent the application code to get and release the persistence context. This is injava EEinJavaPersistence ofAPIa powerful feature that will be further studied later. Now, assuming that the injected value will "do the right Thing" is safe, it does not have to be freed, and it can automatically work with the application server's transaction management.

Introduction of the Persistence unit

can use@PersistenceUnitAnnotations introduce persistence units to yourentitymanagerfactory. As@persistenceContextAnnotations,Unitnameelement to determine what you want to access.entitymanagerfactoryThe persistence unit of the instance. If the persistence unit name is not specified in the annotation, then how to determine if the name will be vendor-specific.

The following code demonstrates how to put aentitymanagerfactoryinstance injected with stateful sessionBeanthe. Then, theBeanwill be inpostconstructA lifecycle callback period is created from the factoryEntitymanagerinstance. Injected byentitymanagerfactoryinstances can be stored securely on any component instance. It is thread-safe when deletingBeaninstance, you do not need to release it.

Code

in the java EE in an environment where persistence units are not frequently used entitymanagerfactory because the injected entity manager is easy to get and use. As you will see later, there is an important difference between the entity manager and the server returned from the factory as the corresponding @PersistenceContext annotations instead of the entity managers provided.

Reference Enterprise JavaBean

when a component needs to accessEJBwhen it takes advantage of@EJBThe annotation statement declares theBeanthe reference. The target of a reference type is usually a sessionBean. Message-drivenBeanThere are no client interfaces, so they are not directly accessible and cannot be injected. We have already demonstratedBeaninterfaceelement that specifies the session to which the client is interestedBean's business interface. The server will search for all deployed sessionsBeanto find the implementation of a requested business interface.

in rare cases, two sessionsBeanimplement the same business interface, or if the client needs access to a differentEJB Jarthe session inBean, you can also specifyBeannameelement that is used to identify the session by its nameBean. A sessionBeanthe name defaults toBeanclass, or it can be set to display to use the@Statelessand theStatefulof the annotationsnameelement.

The following code re-examines the example shown in the above code, this time specifying the value injected Beanname element. sharing the same business interface across multiple bean implementations is not recommended. beanname elements should almost never be required.

Code

Referencing server resources

@Resourceannotations are points to allIMiscellaneous for the Frame element type (catchall) reference, which does not correspond to any of the types described in the gung position. It is used to define references to resource factories, message destinations, data sources, and other server resources. @Resourceannotations are also the easiest to define because the only attached element isResourceType, it allows you to specify the type of resource if the server cannot be specified automatically. For example, if the field you are injecting isObject(object) type, the server has no way of knowing that you want the data source. ResourceTypeelement can be set toJavax.sql.DataSourceTo make the need to be displayed.

@Resourceone of the features of annotations is the ability to obtain logical resources that are specific to the component type. This will includeEjbcontextimplementation and, for example,EJBservices such as timer services. If it is not defined in the same way, it will use theSetterinjected to get in the code aboveEbcontextinstance. In order to complete the example, theSetsessioncontextmethod is placed on the@Resourceannotations. The following code examines the example in the code above, and this time demonstrates using field injection to get aSessioncontextinstance.

Code

Dependency in Java EE--declaring dependencies

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.