When you use EJB3.0 Session bean and message-driven bean (as well as other Java EE5.0 standards), Java Persistence is beginning to reveal its strength. The EJB3.0 specification has been designed to allow persistent integration, because you can obtain Automatic Transaction Division within the Bean method range, or a persistent context (such as Session) that spans the state Session EJB lifecycle ).
This article starts with EJB3.0 and JPA in a managed JavaEE environment. You will learn the basic knowledge by modifying the "Hello World" application. First, you need the Java EE environment-the runtime container that provides the Java EE service.
We recommend that you use JBoss (RedHat an open-source JavaEE server) as a container and server for managing ejbs, supporting the specifications of EJB 1.1, EJB 2.0, and EJB3.0.
Http://sourceforge.net/projects/jboss/files/JBoss/JBoss-4.2.3.GA/
Download the ghost file. After downloading the package, you can directly decompress the file to complete installation. To avoid inexplicable application errors, it is best not to include spaces or Chinese characters in the decompressed path.
Start JBoss
Go to the bin directory of jboss and find the startup script run. bat. Double-click run. bat to start jboss.
If an error occurs during startup, check the following conditions:
1. Whether the ports are occupied, such as 8080 and 1099
2. Whether JAVA_HOME and ClassPath system variables are set for JDK
3. Check whether jdk 1.5 or above is used by jboss. The information can be observed on the jboss console.
4. If the following error occurs during startup: "findstr is not an internal or external command, or a program or batch file that can be run ". Append "% SystemRoot % \ system32; % SystemRoot %;" to the system variable Path ;".
Otherwise, the final solution is to reinstall the JDK on the machine.
In addition, set the JBOSS_HOME system variable for jboss and add the JBOSS_HOME variable to the system variable. The value is the installation path of jboss.
To facilitate the jboss command input, we add the bin directory of jboss to the system path.
Before introducing the EJB component, you must first learn some basic EJB content...
Three bean1 and session bean in EJB)
Interaction with the client is where the business logic is written. bean can directly operate the database through jdbc, but in most cases, operations on the database are performed through the Entity bean.
2. entity bean)
It actually belongs to the java persistence specification (JPA) technology. The emergence of JPA mainly aims to simplify the existing persistence development work and integrate the ORM technology, end the current situation where the ORM frameworks such as Hibernate and TopLink are respectively deployed.
3. message-driven bean)
It is a component dedicated to asynchronous processing of java messages and has the ability to process a large number of concurrent messages.
Session bean
1) stateless Session bean
Normally, we use stateless beans most. Because its bean instances can be used by multiple users, their performance is higher than stateful beans, because a bean instance is used by multiple users, the value set by the previous user may be modified by the previous user, so it cannot properly Save the value set by a user, therefore, it is stateless.
2) stateful Session bean
Stateful beans are usually used by a small number of users, because one bean instance is only used by one user, so the performance overhead is large because its instance is only used by one user, therefore, the value set by the user is not modified by other users. Therefore, you can properly Save the value set by the user, so it is stateful.
How to Develop a stateless session bean1) interface (the interface can be a remote interface or a local Interface)
2) implementation class
Operation steps: 1) Open the IDE development tool (MyEclipse is used)
2) import all jar files in the jboss \ client Directory
3) Compile the interface
package cn.jbit.ejb3.dao;public interface HelloWorldDao {public String sayHelloWorld();}
4) Write implementation classes
Package cn. jbit. ejb3.dao. impl; import javax. ejb. remote; import javax. ejb. stateless; import cn. jbit. ejb3.dao. helloWorldDao; @ Stateless // Stateless Session bean @ Remote (HelloWorldDao. class) // remote interface public class HelloWorldDaoImpl implements HelloWorldDao {public String sayHelloWorld () {return "HelloWorld ";}}
5) package
File-> Export-> Java-> JAR file,
6) Release
Copy the packaged jar package to jbZ container? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3Ncc2VydmVyXGRlZmF1bHRcZGVwbG95vfjQ0MjIsr/placement =" brush: java; "> package cn. jbit. ejb3.test; import javax. naming. initialContext; import javax. naming. namingException; import cn. jbit. ejb3.dao. helloWorldDao; public class Test {public static void main (String [] args) {try {InitialContext ctx = new InitialContext (); HelloWorldDao dao = (HelloWorldDao) ctx. lookup ("HelloWorldDaoImpl/remote"); System. out. println (dao. sayHelloWorld ();} catch (NamingException e) {e. printStackTrace ();}}}Note: add the jndi. properties (JNDI Generation Policy) to the src directory)
The content is as follows:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactoryjava.naming.provider.url=localhost:1099
The default JNDI name generated by JBoss
When an EJB is published to JBoss, if we do not specify a global JNDI name for it or modify its default EJB name, JBoss generates a global JNDI name for the EJB according to the default naming rules, the default naming rules are as follows:
If you package an EJB application into a module File suffixed with *. jar, the default global JNDI name is
1) local interface: EJB-CLASS-NAME/local
2) remote interface: EJB-CLASS-NAME/remote
If you package EJB as a module into a java ee Enterprise Application file with the suffix *. ear, the default global JNDI name is
1) local interface: EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local
2) remote interface: EAR-FILE-BASE-NAME/EJB-CLASS-NAME/remote
EAR-FILE-BASE-NAME is the NAME of the ear FILE, and the EJB-CLASS-NAME is the unqualified Class NAME of EJB.
Summary:
This article only mentions ejbs to minimize the difficulty.
The next article references the EJB component in JPA.