EJB3.0 Development Guide: Dependent Injection

xiaoxiao2021-03-06  48

Dependency Injection, also known as control reversal (IOC), originally a design model, now being a noisy hot and chair, a little over. More famous items such as String, PicoContainer, etc.

In EJB3.0, you can use the annotation injection dependence on the field and setting method, I want to have new projects soon, or on the basis of the original project, some IOC containers will use annotations to inject dependence. The annotated japanese JDK5.0 is indeed a very powerful feature, relatively, the attributes in .NET have not played so much value. This is the power of open source, thousands of for the open source Java programmers continue to emerge in new ideas new features.

Look at the following example: We know, a data source has been configured by default in JBoss, and its JNDI name is "Java: / Defaultds". Below this example declares a data source, by comment, you can assign this default data source for JBoss to it.

@Resource (JADINAME = "Java: / DefaultDS") Public DataSource Customerdb;

Resource annotation

@Target ({TYPE, METHOD, FIELD, PARAMETER}) @Retention (RUNTIME) public @interface Resource {String name () default ""; String resourceType () default ""; AuthenticationType authenticationType () default CONTAINER; boolean shareable () DEFAULT TRUE; STRING JNDINAME () Default "";} public enum authentication type {container, application} @Target (TYPE) @ReT1 (Runtime) public @interface resources {resource [] value ();

Resource's Name points to a resource named in the environmental properties, is used to specify whether the container or EJB components are authenticated, and Sharebale specifies whether to share, JNDINAME is used to specify the name in JDNI. ResourceType () is used to specify the type of resource.

If Name and ResourceType point to the commented program, AuthenticationType and Resourcetype are default, you can use the INJECT annotation:

@Inject (jndiname = "java: / defaultds") Public DataSource Customerdb;

For members of the single case, you can simplify:

@Inject javax.ejb.sessionContext CTX; @Inject javax.ejb.timervice timer; @Inject javax.ejb.usertransaction ut; @Inject javax.ejb.entityManager manager;

Resources comments can inject multiple resources.

The example di provided herein is imported in Eclipse.

This example reads the JMS_USER table from the database and displays the contents in the table. This example uses the dependent implantation to get JBoss default data sources.

This example has main five files:

JMSUSERS.JAVA: Business Interface.

JMSUSERSBEAN.JAVA: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Here is a description of 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 getusers ();

This interface is simple, defined a method of getting all users, JBoss defaults to use the full name of the interface as its JNDi name. In the above example, its full name can pass through JMSUSERS.CLASS. GetName () is obtained.

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 getUsers () {List list = new ArrayList (); try {Connection conn = customerDB.getConnection (); Statement st = conn.createStatement (); ResultSet rs = st.executeQuery ( "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 is injected into the JBoss data source into the CustomerDB variable, so don't think that CustomerDB is not initialized, these work is done.

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 = (jmsusers) ctx.lookup (jmsusers.class.getname ()); list jms = 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 EJB component we publish. Displays the data read from the JMS_USERS table.

Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, execute Ant Ejbjar, and release this EJB to execute the Run Target in the Eclipse's ANT view. Or in the command line, enter this project directory, perform Ant Run, test this EJB

转载请注明原文地址:https://www.9cbs.com/read-81470.html

New Post(0)