2017-05-24 22 views
0

我有一个EAR,它目前只包含一个WAR。我的问题是,如果mybatis -.... jar文件位于web-module.war \ WEB-INF \ lib然后一切工作正常。但如果我用maven创建了一场小型战争,并且两个mybatis相关的jar文件位于EAR/lib下,我得到一个“没有正确配置SqlSessionFactory生产者。”“错误。EAR情况下MyBatis CDI类加载器问题

我认为这是一个类路径相关的问题。

我打算在EAR中加入更多的WAR,并且我想避免将这两个罐放入每个WAR中。

环境:Java的8,似鲭水狼牙鱼服务器4.1.2.172

这是我的耳朵的结构,但它不工作

EAR 
    +-- lib 
     + mybatis-3.4.4.jar 
     + mybatis-cdi-1.0.0.jar 

    +-- web-module.war 

Web模块包含一个EJB注入一个servlet 。这个EJB使用myBatis映射器。

的Servlet

@WebServlet("/echo") 
public class EchoServlet extends HttpServlet { 
    @EJB 
    private EchoService echoService; 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
     resp.setContentType("text/html"); 
     try (PrintWriter writer = resp.getWriter()) { 
      writer.println(echoService.echo("lorempisum")); 
     } 
    } 
} 

EJB

@Stateless 
public class EchoBean implements EchoService { 

    @Inject 
    private AccountDao accountDao; 

    @Override 
    public String echo(String text) { 
     List<String> emails = accountDao.findAll(); 
     return "Hello " + text + "! There are " + emails.size() + " emails in the DB."); 
    } 
} 

MyBatis的映射器

@Mapper 
public interface AccountDao { 
    @Select("SELECT email FROM dev.account") 
    List<String> findAll(); 
} 

如果我更改了EAR的结构,那么我的项目工作正常。

EAR 
    +-- lib 

    +-- web-module.war 
     +-- WEB-INF 
      +-- lib 
       +-- mybatis-3.4.4.jar 
       +-- mybatis-cdi-1.0.0.jar 

我尝试添加MANIFEST.MF文件耳\ WEB-module.war \ META-INF目录用下面的内容,但它并没有帮助:

Class-Path: lib/mybatis-3.4.4.jar lib/mybatis-cdi-1.0.0.jar 

我也试着添加glassfish-web.xml文件到ear \ web-module.war \ WEB-INF所描述的目录here但它并没有帮助:

<glassfish-web-app> 
    <class-loader delegate="true" extra-class-path="../lib/mybatis-3.4.4.jar:../lib/mybatis-cdi-1.0.0.jar"/> 
</glassfish-web-app> 

不知道如何解决这个问题的类路径?

回答

0

我不记得是否它是我们最终达成此目的的原因,但是:解开EAR组件(简单的文件夹用于耳内档案中的战争和ejbs)可能会有所帮助。

这里是与Maven的配置:

<plugin> 
    <artifactId>maven-ear-plugin</artifactId> 
    <configuration> 
     <applicationName>myApp</applicationName> 
     <version>6</version> 
     <filtering>true</filtering> 
     <initializeInOrder>true</initializeInOrder> 
     <defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir> 
     <unpackTypes>war,ejb</unpackTypes> 
     <modules> 
      <ejbModule> 
       <groupId>com.example</groupId> 
       <artifactId>myapp-ejb</artifactId> 
       <bundleFileName>myapp-ejb.jar</bundleFileName> 
      </ejbModule> 
      <webModule> 
       <groupId>com.example</groupId> 
       <artifactId>myapp-rest</artifactId> 
       <contextRoot>/myapp</contextRoot> 
       <bundleFileName>myapp.war</bundleFileName> 
      </webModule> 
     </modules> 
    </configuration> 
</plugin>