Dependency in JavaEE-declare dependency and javaee dependency
The following summary describes some resource annotations defined by Java EE and EJB specifications. Each annotation has an optional name attribute to specify the reference name of the dependency. Other features of the annotation are specific to the resource type to be obtained.
Reference persistence Context
The previous section demonstrates how to create an Entity Manager for a persistent context by returning an EntityManagerFactory from the Persistence class. In the JavaEE environment, the @ PersistenceContext annotation can be used to declare dependencies on a persistent context and automatically obtain the Entity Manager required for that persistent context.
The following code demonstrates how to use the @ PersistenceContext annotation to obtain an Entity Manager through dependency injection. The unitName element specifies the name Of the persistence unit based on the persistence context.
Tip: If the unitName element is ignored, determine how the unit name used for persistent context is vendor-specific. If an application has only one persistent unit, some vendors can provide default values, while others may require that the unit name be specified in a vendor-specific configuration file.
(CODE)
After receiving a warning that the Status field is used in a stateless Session bean, you may want to know how to ensure that the Code is legal. After all, the Entity Manager must maintain its own State to be able to manage specific persistence contexts. The good news is that the JPA specification is designed for Java EE integration, so unlike the previous chapter, the code injected in the Middle Ages is not an Entity Manager instance. The bean injection value is a container-hosted proxy that represents the application code to obtain and release the persistent context. This is a powerful function of Java persistence API in JavaEE, which will be further studied later. Now, it is assumed that the injection value is safe to "do the right thing" and does not have to be released, and it can automatically work with the transaction management of the application server.
Introduce persistence units
You can use the @ PersistenceUnit annotation to introduce your EntityManagerFactory. As with the @ persistenceContext annotation, The unitName element determines the persistent unit of the EntityManagerFactory instance to be accessed. If the persistent unit name is not specified in the annotation, determine how the name will be vendor-specific.
The following code demonstrates how to inject an EntityManagerFactory instance into a stateful Session bean. The bean then creates an EntityManager instance from the factory during the PostConstruct lifecycle callback. The injected EntityManagerFactory instance can be securely stored on any component instance. It is thread-safe and does not need to be released when the bean instance is deleted.
(CODE)
In the JavaEE environment, EntityManagerFactory of persistence unit is not frequently used, because it is easy to obtain and use the injection Entity Manager. 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 annotation but the Entity Manager provided.
Reference Enterprise JavaBean
When a component needs to access EJB, it uses the @ EJB annotation to declare a reference to the bean. The target of the reference type is usually a session bean. Message-driven beans do not have client interfaces, so they cannot be accessed directly and cannot be injected. We have demonstrated the beanInterface element to specify the business interface of the session bean that the client is interested in. The server searches all deployed session beans to find the implementation of a requested business interface.
In rare cases, two session beans implement the same service interface, or if the client needs to access the Session bean located in different EJB jar, you can also specify the beanName element, identifies the session bean by its name. By default, the name of a session bean is the non-qualified class name of the bean class, or you can set it to the name element explicitly annotated with @ Stateless and Stateful.
The following code re-examines the example shown in the above Code. This time, the beanName element is specified on the injected value. Sharing the same service interface across multiple beans is not recommended. BeanName elements should have almost never been asked.
(CODE)
Reference server resources
@ Resource annotation is a miscellaneous (catchall) Reference pointing to all I frame metadata types. It does not correspond to any type described by the starting position. It is used to define the reference of the resource factory, Message destination, data source, and other server resources. @ Resource annotation is also the easiest to define, because the only appended element is resourceType. If the server cannot be automatically specified, it allows you to specify the Resource type. For example, if the field you are injecting is of the Object type, the server cannot know that you want the data source. The resourceType element can be set to javax. SQL. DataSource to display the resource.
@ Resource annotation can be used to obtain logical resources of specific component types. This includes EJBContext implementation and services such as the EJB timer service. If it is not defined similarly, setter injection will be used to obtain the EBContext instance in the code above. To complete this example, the @ Resource annotation will be placed on the setSessionContext method. The following code re-examines the example in the above Code. This shows how to use field injection to obtain a SessionContext instance.
(CODE)