【使用JBoss 7开发Java EE 6】EJB引用第三方包

时间:2021-05-22 20:57:26

在EJB里引用了第三方包后,必须在JBoss里进行这个包的配置,否则项目肯定会部署失败。
在JBoss7以前的版本里,这些第三包可以放到%JBOSS_HOME%/common/lib下面,但JBoss 7与以前的版本完全不同了。JBoss 7里使用的是模块加载,第三方Jar包也会当成模块来加载,所以需要在JBoss 7里对第三方Jar包进行配置。

配置分为两个步骤,一个是在modules这个文件夹里以包层次作为文件夹名,最后一级是版本号,没有则为main,如示例:

package calculate;

public class Calculate {

    public int add(int a,int b){
        return a+b;
    }

    public int minus(int a,int b){
        return a-b;
    }

}

这是一个简单的Calculate类,所属包为calculate,只有一级,所以在modules文件夹里层次如下:
modules –
    calculate –
        main –
            Calculate.jar    Calculate.jar.index     module.xml

如上图,我们还需三个文件:一个需要使用的jar包,一个是以jar包名称为前缀,.index为后缀的文件,最后一个是module.xml文件

Calculate.jar.index的内容如下:

META-INF
calculate

它包含的是需用到的包名(这个文件可不要,当我们完成下一步后,它会自动生成)

module.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2010, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<module xmlns="urn:jboss:module:1.0" name="calculate">
  <resources>
    <resource-root path="Calculate.jar"/>
        <!-- Insert resources here -->
  </resources>

  <dependencies>
  </dependencies>
</module>

重点是后面name属性和resources子节点和dependecies,前者放置jar包文件名,后者放置此jar包的依赖项。依赖项的放置如下:

 <dependencies>
        <module name="javax.transaction.xa" slot="main"/>
    </dependencies>

上述几个文件准备好后,就到standalone\configuration文件夹下,打开standalone.xml
找到(113行左右),在此节点添加代码,
最终节点内容如下:

        <subsystem xmlns="urn:jboss:domain:ee:1.0">
            <global-modules>
                <module name="calculate" slot="main"/>
            </global-modules>
        </subsystem>

加完后,重启服务器就可以再次部署EJB了。