JAVA: dependency injection in EJB 3.0 Development Guide

Source: Internet
Author: User
Tags jboss

Dependency Injection, also known as IOC, is a design pattern. It is a bit too popular. Well-known projects such as String and picoContainer.

In EJB3.0, you can add annotations to inject dependencies into fields and settings. I want to have a new project soon, or on the basis of the original project, some IOC containers use annotations to inject dependencies. The annotations added by JDK are indeed a powerful function. the attributes in. net do not play that great value. This is the power of open source. Thousands of open source java programmers are constantly emerging with new ideas and new functions.

Let's take a look at the example below: we know that a data source has been configured by default in JBOSS, and its JNDI name is "java:/DefaultDS ". The following example declares a data source. By commenting, you can assign the default data source of JBOSS to it.

@ Resource (jndiName = "java:/DefaultDS ")
Public DataSource customerDB;
Resource annotation Declaration
@ Target ({TYPE, METHOD, FIELD, PARAMETER}) @ Retention (RUNTIME)

Public @ interface Resource {

String name () default "";

String resourceType () default "";

AuthenticationType authenticationType () default CONTAINER;

Boolean retriable () default true;

String jndiName () default "";

}


Public enum Authentication Type {

CONTAINER,

APPLICATION

}

@ Target (TYPE) @ Retention (RUNTIME)

Public @ interface Resources {

Resource [] value ();

}

The Resource name points to a Resource named in the Environment attribute. AuthenticationType is used to specify the container or EJB component for identity authentication, sharebale is used to specify whether to share, and jndiName is used to specify the name in JDNI. ResourceType () is used to specify the resource type.

If both name and resourceType point to the annotated Program Member, both AuthenticationType and resourceType are default, you can use Inject annotation:

@ Inject (jndiName = "java:/DefaultDS ")

Public DataSource customerDB;

For single-instance members, you can simplify the process:

@ Inject javax. ejb. SessionContext ctx;

@ Inject javax. ejb. TimerService timer;

@ Inject javax. ejb. UserTransaction ut;

@ Inject javax. ejb. EntityManager manager;

Resources annotation can inject multiple Resources.

Import the example DI provided in this article in Eclipse.

This example reads the JMS_USER table from the database and displays the table content. This example uses dependency injection to obtain the default JBOSS data source.

This example mainly contains five files:

JmsUsers. java: business interface.

JmsUsersBean. java: Business implementation class. In the future, the EJB we developed will also be named like this (add Bean to the interface name ).

Client. java: Client class for testing EJB.

Jndi. properties: The jndi property file that provides basic configuration properties for accessing jdni.

Build. xml: ant configuration file for compiling, publishing, testing, and clearing ejbs.

The following describes the content of each file.

JmsUsers. java

Package com. kuaff. ejb3.di;
Import java. util. List;
Import javax. ejb. Remote;
Import javax. SQL .*;

@ Remote
Public interface JmsUsers
{
Public List <String> getUsers ();
}

This interface is simple and defines a method for getting all users. By default, JBOSS uses the full name of the interface as its JNDI name. In the preceding example, the full name of JmsUsers. class can be used. GetName.

CounterBean. java

Package com. kuaff. ejb3.di;
Import java. util. List;
Import java. util. ArrayList;
Import javax. ejb. Stateless;
Import javax. ejb. Resource;
Import javax. SQL .*;
Import java. SQL .*;


@ Stateless
Public class JmsUsersBean implements JmsUsers
{
@ Resource (jndiName = "java:/DefaultDS", resourceType = "javax. SQL. DataSource ")
Public DataSource customerDB;
Public List <String> getUsers ()
{
List <String> list = new ArrayList <String> ();
Try
{
Connection conn = customerDB. getConnection ();
Statement st = conn. createStatement ();
ResultSet rs = st.exe cuteQuery ("select * from jms_users ");
While (rs. next ())
{
List. add (rs. getString ("userid "));
}
}
Catch (SQLException e)
{}
Return list;
}
}

This is the specific implementation of business logic. Once this EJB is generated by the container, the container injects the JBOSS data source into the customerDB variable, so do not think that customerDB is not initialized. These tasks are done by the container.

Client. java

Package com. kuaff. ejb3.di;
Import java. util. List;
Import javax. naming. InitialContext;
Import javax. naming. NamingException;

Public class Client
{
Public static void main (String [] args)
{
InitialContext ctx;
Try
{
Ctx = new InitialContext ();
JmsUsers users = (JmsUsers) ctx. lookup (JmsUsers. class. getName ());
List <String> jmsUsers = users. getUsers ();
For (String user: jmsUsers)
{
System. out. printf ("user name: % s % n", user );
}
}
Catch (NamingException e)
{
E. printStackTrace ();
}
}
}

This class is used to test the released EJB component. Displays the data read from the JMS_USERS table.

Run. bat: run? In the {$ JBOSS_HOME}/bin directory? Call to start JBOSS.

Execute ejbjar target in the Ant view of Eclipse. Alternatively, go to the project directory under the command line and execute ant ejbjar to package the compilation and release the EJB.

Run target in the Ant view of Eclipse. Or enter the project directory under the command line and run ant run to test the EJB.

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.