如果您想将自定义 JNDI 工厂添加到 Wildfly,请使用https://github.com/juanlmelo/mongo-jndi-plugin/
您需要做几件事:
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驱动
-create 目录:wildfly-8.1.0.Final/modules/com/mongodb/driver/main
-create there file: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"