Ejb3.0 dependency Injection depth 2

Source: Internet
Author: User
Tags jboss application server

Dependency injection, also known as IOC, is the latest development trend. IOC containers, such as spring, become popular because they simplify the obfuscation of Enterprise Java, most of which come from JNDI. In this article, I will discuss how to use dependency injection to declare resources and services in the forthcoming Java EE 5.0. I will use ejb3.0, Web Servic metadata and dependency injection to port the j2ee1.4 blueprint program Java advanced compiler. In this article, I will use this application to describe these concepts.

Copyright Disclaimer: Any website authorized by matrix must retain the following author information and links for reprinting.
Author: esunyang (author's blog: http://blog.matrix.org.cn/page/EsunYang)
Original article: http://www.matrix.org.cn/resource/article/44/44321_Dependency+Injection.html
Keyword: dependency; Injection

What is dependency injection?

Most enterprise-level Java applications use external resources and services such as datasources, ejbs, and WebService. in J2EE 1.4, you must explicitly declare the dependent resources in the deployment descriptor or use the lookup method of JNDI to obtain the resource reference.

For example, if you want to use a service such as datasource or EJB in J2EE 1.4, you must make a definition similar to the following in the deployment descriptor:

 
  
         ejb/HelloWorld
         oracle.ejb30.HelloWorld

Then, before using the following resources, you must use JNDI to search for this object as follows:

Context ic = new InitialContext();
HelloWorld helloWorld = (
    HelloWorld)ic.lookup("java:comp/env/ejb/HelloWorld");

These methods not only make Java beginners feel hard to understand, but also are easy to mislead. All of these are attributed to the complexity of J2EE.
Dependency injection is opposite to JNDI. It (dependency injection) allows you to declare dependencies. When you need to request resources, the Java EE container processes the complex instantiation and initialization of resources or services. Based on the Resource declaration using the release or deployment descriptor, a resource instance is inserted into the Java EE 5.0 container when necessary. Figure 1 compares JNDI with dependency injection:


Figure 1. Comparison between JNDI and dependency Injection

Where to use dependency injection?

Dependency injection can only be used by managed objects. Managed Objects are objects managed by Java EE containers, such as ejbs or Servlets, rather than helper classes). For example, if we have an EJB, we can use dependency injection in ejb3.0 instead of using dependency injection in the helper class on which it depends. The following table lists the types of dependency injection supported by the web and EJB modules:

Some Java EE containers, such as Oracle Application Server 10g 10.1.3 and JBoss Application Server 4.0, provide earlier support for ejb3.0. Therefore, they support dependency injection in EJB containers.

As described above, you can use metadata to describe or deploy a description to declare dependencies on resources. In the recently submitted final jsr250, two resource dependency annotation classes (javax. annotation. resource and javax. annotation. resources). In ejb3.0 of jsr200, javax is defined for the dependency solution of ejbs. EJB. java API for XML Web Service 2.0 defines javax. XML. WS. webserviceref.

Resource annotations can be used for managed classes such as EJB or servlet, or methods and fields in them (managed classes. You can use resource dependencies to determine the dependencies on resources of any category, such as datasource, JMS, mail, URL, or environment enries ).
The following is the statement for resolving a javax. annotation. Resource interface:

public @interface Resource {    
public enum AuthenticationType {CONTAINER,APPLICATION
    }    
String name() default '';    
Class type() default Object.class;    
AuthenticationType authenticationType()
default  AuthenticationType.CONTAINER;    
boolean shareable() default true;
String mappedName default '';
description() default '';
}

The following table describes the parameters that can be specified in javax. annotation. Resource:

Ingress input type
Java EE 5.0 supports two types of delimiter entry: Field and setter. Field setter inbound allows you to merge resources into a field, while setter inbound (set setter inbound) allows you to call the setter method to merge resources.

To use field fields, you only need to define a field and parse the resources it references. If you do not define the referenced Resource Name and type, the container will inherit information from the field name and type. For example, you can parse datasource as follows ):

@Resource 
private javax.sql.DataSource AdventureDB;

In the above example, you should configure a datatsource region with the adventuredb JNDI name. If not, an exception will occur. Before a customer can access a managed object, the dependent ingress must be completed.

In contrast, setter (or property setter) allows you to define a set... () Method, and annotate it as a resource reference. The application does not need to call this setter method. The container will call this setter Method Used for ingress resources before calling the business method. For example, if you have a field named adventuredb, you must have a setter method named setadventuredb. For example, if you do not define the name and type of the setter, "setter" will inherit information from the setter method name and parameter type. For example, the following is a setter Method for referencing resources:

@Resource 
private void setAdventureDB(javax.sql.DataSource ds)
{
adventureDB = ds;
}
private DataSource adventureDB;

Let's take a look at some typical usage of using the resource injection solution for dependency injection in Java EE applications.

Use resource injection for dependency Injection

Use Data sources
Data sources are frequently used in Java applications. You can use the dependency ingress instead of using JNDI to obtain a data source. For example, if you have a data source named JDBC/adventuredb in your environment, you can reference the data source as follows:

@Resource(name="jdbc/AdventureDB") 
private javax.jdbc.DataSource myDB;
Connection con;
con = myDb.getConnection();

Use JMS Resources
Java Message Service (JMS) makes message-oriented middleware (MOM) very easy to use and is widely used in Java EE applications. You can use resource annotations to insert a JMS target such as a queue or topic, or connect factory resources. For example, if you want to use a JMS resource, you first create it on the server configuration, and then use the @ resource annotation below to declare a dependency:

@Resource(name="jms/WorkFlowManagerQueue")
private Queue wfmQueue;

Use environment entries
Environment entries allow you to specify a business parameter that varies by time or environment. For example, if you want to set the maximum number of transactions that a user can trade per month in your application, it is meaningless to perform hard encoding in the application, because you want to change its value in the future, or you want to use different values to test your product system.
For example, if you want to set the maxtradesallowedperuser value to 50, you can declare it as follows:

@Resource int maxTradesAllowedPerUser = 50

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.