EJB会话bean在EAR包装中没有绑定在JBoss 5中

时间:2018-01-16 14:33:17

标签: ant jboss ejb ear

我有一个动态的Web项目,我在JBoss 5上以EAR格式部署。使用ant我在应用服务器上编译和部署ear文件。如果我独立部署EJB和war(客户端),那么一切正常,但是当我尝试打包时,会话bean没有绑定。我的EJB代码和客户端(Web)在同一个项目中我没有项目中的独立EJB模块。我尝试在WEB-INF lib中添加jar的业务接口,甚至在ear文件的lib目录中,但没有任何效果。我想知道如果EJB jar不在类路径中以及如何在ear文件中设置模块的顺序?

application.xml中

    <?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="Application_ID" version="5">
  <display-name>X3</display-name>
  <module>
    <web>
      <web-uri>x3.war</web-uri>
      <context-root>x3</context-root>
    </web>
  </module>
  <module>
        <ejb>x3.jar</ejb>
    </module>
</application>

的build.xml

<?xml version="1.0"?> 
<!DOCTYPE xml>
<project name="x3" default="all" basedir=".">

    <property name="compiledWebClasses" value="${basedir}/build/classes" />
    <property name="dist" value="${basedir}/dist" />
    <property name="descriptors" value="${basedir}/dd" />
    <property name="build" value="${basedir}/build" />

    <property name="JBOSS_CLIENT_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\client" />
    <property name="JBOSS_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\lib" />
    <property name="JBOSS_COMMON_LIB" value="C:\jboss5dummy\jboss-5.0.1.GA\common\lib" />


    <path id="compile.classpath">
        <fileset dir="${JBOSS_CLIENT_LIB}">
            <include name="*.jar"/>
        </fileset>

        <fileset dir="${JBOSS_LIB}">
                    <include name="*.jar"/>
                </fileset>

            <fileset dir="${JBOSS_COMMON_LIB}">
                        <include name="*.jar"/>
                    </fileset>
    </path>

    <target name="clean">
        <delete dir="${basedir}/build" />
        <delete dir="${basedir}/dist" />
    </target>

    <target name="createFolders">
        <mkdir dir="${basedir}/build/classes" />
        <mkdir dir="${basedir}/dist" /> 
    </target>

    <target name="compile">
        <javac srcdir="src" destdir="build/classes" debug="true">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>

    <target name="jar">
            <jar jarfile="${build}/x3.jar">
                <fileset  dir="${compiledWebClasses}">
                        <include name="**/ejb/**"/>
                </fileset>
                <metainf dir="${descriptors}">
                    <include name="persistence.xml"/>
                </metainf>
            </jar>
    </target>

    <fileset dir="${compiledWebClasses}" id="warClasses.fileset">
        <include name="**/controller/**" />
    </fileset>

    <target name="war">
        <war destfile="${build}/x3.war" webxml="${basedir}/WebContent/WEB-INF/web.xml">
            <fileset dir="${basedir}/WebContent"/>
            <classes refid="warClasses.fileset"/>
        </war>
    </target>

    <target name="ear">
            <jar destfile="${dist}/x3.ear">
                <metainf dir="${descriptors}">
                    <include name="application.xml"/>
                    <include name="jboss-app.xml"/>                
                 </metainf>
                <fileset file="${build}/x3.war"/>
                <fileset file="${build}/x3.jar"/>

            </jar>
        </target>

    <target name="all" depends="clean, createFolders, compile, jar, war, ear">
        <echo message="done" />
    </target>

</project>

的jboss-app.xml中

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jboss-app>
    <loader-repository>dev.lmd.com:loader=x3.ear</loader-repository>
</jboss-app>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>  
<persistence version="2.0"  
 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">  
 <persistence-unit name="StudentMgtPU"  
  transaction-type="JTA">  
  <jta-data-source>java:/StudentMgtDS</jta-data-source>  
  <properties>  
   <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />  
   <property name="hibernate.hbm2ddl.auto" value="create" />  
  </properties>  
 </persistence-unit>  
</persistence>

业务接口

package com.lmd.dev.ejb.session;  

import javax.ejb.Local;  

import com.lmd.dev.ejb.domain.Student;  

@Local  
public interface ManageStudentSessionBeanLocal {  
 public boolean addStudent(Student Student);  
} 

业务逻辑

package com.lmd.dev.ejb.session;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lmd.dev.ejb.domain.Student;

/**
 * Session Bean implementation class ManageStudentSessionBean
 * 
 * @author Sameera Jayasekara
 */
@Stateless
public class ManageStudentSessionBean implements ManageStudentSessionBeanLocal {

 @PersistenceContext
 private EntityManager entityManager;

 public boolean addStudent(Student student) {
     System.out.println("Trying .............");
  entityManager.persist(student);
  return true;
 }

}

Servlet(客户端)

package com.lmd.dev.controller;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lmd.dev.ejb.domain.Student;
import com.lmd.dev.ejb.session.ManageStudentSessionBeanLocal;

/**
 * 
 * 
 * 
 */
public class ManageStudentServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

 @EJB(mappedName="ManageStudentSessionBean/local")
 private ManageStudentSessionBeanLocal manageStudentSessionBeanLocal;

 public void init(ServletConfig config) throws ServletException {

  super.init(config);

 }

 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {

 }

 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {

  String message = "";

  String firstName = request.getParameter("fname");
  String lastName = request.getParameter("lname");
  String email = request.getParameter("email");

  Student student = new Student();
  student.setFirstName(firstName);
  student.setLastName(lastName);
  student.setEmail(email);

  if (manageStudentSessionBeanLocal != null && manageStudentSessionBeanLocal.addStudent(student)) {
      message = "Student Successfuly Added";
  } else {
      message = "Student Adding Failed";
  }

  request.setAttribute("message", message);
  RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
  rd.forward(request, response);

 }

}

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>x3</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ManageStudentServlet</display-name>
    <servlet-name>ManageStudentServlet</servlet-name>
    <servlet-class>com.lmd.dev.controller.ManageStudentServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ManageStudentServlet</servlet-name>
    <url-pattern>/ManageStudentServlet</url-pattern>
  </servlet-mapping>
</web-app>

您也可以在git上找到源代码

Source Code

1 个答案:

答案 0 :(得分:1)

当我们从EJB模块中打包EAR文件时,客户端的查找方法稍有改变。我们总是可以在控制台中检查它应该引用什么路径进行注入。在Servlet for EAR打包中查找EJB注入的方法就是这样的。

tb3