如果您想使用 https://github.com/juanlmelo/mongo-jndi-plugin/ 将自定义 JNDI 工厂添加到 Wildfly,您需要做一些事情:
1) 你需要改变一点https://github.com/juanlmelo/mongo-jndi-plugin/blob/master/src/main/java/com/mongodb/jndi/MongoClientJNDIFactory.java 的实现
- 第 38 行更改为:
String mongoURI = (String) environment.get(MONGO_CLIENT_URI);
并注释或删除第 39-49 行
2) 然后运行命令mvn clean package 并创建目录,例如:
wildfly-8.1.0.Final/modules/com/mongodb/jndi/main/
复制mongo-jndi-plugin-1.0.jar 并在那里创建带有内容的module.xml 文件:
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="com.mongodb.jndi">
<resources>
<resource-root path="mongo-jndi-plugin-1.0.jar"/>
</resources>
<dependencies>
<module name="com.mongodb.driver"/>
<module name="javax.api"/>
</dependencies>
</module>
3) 添加mongo驱动
-创建目录:wildfly-8.1.0.Final/modules/com/mongodb/driver/main
-在那里创建文件:module.xml 并放置在那里:
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="com.mongodb.driver">
<resources>
<resource-root path="mongo-java-driver-2.11.0.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
-添加到这个目录mongo-java-driver-2.11.0.jar库
4) 添加类似这样的内容
<subsystem xmlns="urn:jboss:domain:naming:2.0">
<bindings>
<object-factory name="java:global/MongoClient" module="com.mongodb.jndi" class="com.mongodb.jndi.MongoClientJNDIFactory">
<environment>
<property name="mongoClientURI" value="mongodb://username:password@yourdomain.com:27017,username:password@yourdomain.com:27017"/>
</environment>
</object-factory>
</bindings>
<remote-naming/>
</subsystem>
致您的domain.xml 或standalone.xml
瞧
在此步骤之后,您可以使用 @Resource 注释将 MongoClient 注入到您的类中(例如,@Resource(lookup = "java:global/MongoClient"