5
A
回答
0
如果你的意思是像JDBC访问常规的RDBMS,那么答案是否定的。
2
要做到这一点,您需要一个适用于MongoDB的JDBC驱动程序impl。我只找到了一个,它从MongoDB页面被称为“实验”:GitHub JDBC Driver for MongoDB。
to workaroud this limit,you can setup some Spring beans and create a MongoDB implementation for your application DAO(这样,你不需要改变DAO接口和它的客户端组件)。
此文章可以帮助:
11
是的,它是可能的,为什么有人依靠别人的代码时,你可以创建自己的JNDI工厂? 只需创建一个实现javax.naming.spi.ObjectFactory的类以及一个从JNDI上下文中取出mongo的bean,我将其配置为spring data-mongo MongoTemplate对象。
public class CustomMongoJNDIFactory implements ObjectFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable<?, ?> environment) throws Exception {
validateProperty(obj, "Invalid JNDI object reference");
MongoTemplate mongoTemplate = null;
String db = null;
String host = null;
String username = null;
String password = null;
int port = 27017;
Reference ref = (Reference) obj;
Enumeration<RefAddr> props = ref.getAll();
while (props.hasMoreElements()) {
RefAddr addr = (RefAddr) props.nextElement();
String propName = addr.getType();
String propValue = (String) addr.getContent();
if (propName.equals("db")) {
db = propValue;
} else if (propName.equals("host")) {
host = propValue;
} else if (propName.equals("username")) {
username = propValue;
} else if (propName.equals("password")) {
password = propValue;
} else if (name.equals("port")) {
try {
port = Integer.parseInt(propValue);
} catch (NumberFormatException e) {
throw new NamingException("Invalid port value " + propValue);
}
}
}
// validate properties
validateProperty(db, "Invalid or empty mongo database name");
validateProperty(host, "Invalid or empty mongo host");
validateProperty(username, "Invalid or empty mongo username");
validateProperty(password, "Invalid or empty mongo password");
//create mongo template
mongoTemplate = new MongoTemplate(new Mongo(host, port), db,
new UserCredentials(username, password));
return mongoTemplate;
}
/**
* Validate internal String properties
*
* @param property
* @param errorMessage
* @throws NamingException
*/
private void validateProperty(String property, String errorMessage)
throws NamingException {
if (property == null || property.trim().equals("")) {
throw new NamingException(errorMessage);
}
}
/**
* Validate internal Object properties
*
* @param property
* @param errorMessage
* @throws NamingException
*/
private void validateProperty(Object property, String errorMessage)
throws NamingException {
if (property == null) {
throw new NamingException(errorMessage);
}
}
}
的Spring bean:
@Configuration
@Qualifier("mongoTemplate")
public class CustomMongoTemplate {
public @Bean MongoTemplate mongoTemplate() throws Exception {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (MongoTemplate) envCtx.lookup("bean/MyMongoBean");
}
}
的context.xml:
<Resource name="bean/MyMongoBean" auth="Container"
type="org.springframework.data.mongodb.core.MongoTemplate"
factory="com.package.CustomMongoJNDIFactory"
host="" db="" username="" password=""/>
的web.xml
<resource-env-ref>
<description>Mongo JNDI configuration</description>
<resource-env-ref-name>comp/env/bean/MyMongoBean</resource-env-ref-name>
<resource-env-ref-type>org.springframework.data.mongodb.core.MongoTemplate</resource-env-ref-type>
</resource-env-ref>
-1
还有一个努力是为MongoDB提供一个JDBC驱动程序impl。在这里:
https://sourceforge.net/projects/mongojdbcdriver
的任何措施不完整,但将有希望提供一个JDBC实现,很快熟悉Java开发人员。
3
重用胡安·安东尼的ObjectFactory
接口(CustomMongoJNDIFactory
)的定制实现,它也可以在context.xml file
使用Spring的jee
命名空间的JNDI的查找标签和相应的Tomcat的配置配置,这样的:
spring-mongodb-persistence-context.xml
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<jee:jndi-lookup id="mongoTemplate" jndi-name="java:/comp/env/jndi/MongoDB" expected-type="org.springframework.data.mongodb.core.MongoTemplate" />
<mongo:repositories base-package="com.package.repository.mongodb" />
</beans>
context.xml
:
<Resource name="jndi/MongoDB"
auth="Container"
type="org.springframework.data.mongodb.core.MongoTemplate"
factory="com.package.mongo.CustomMongoJNDIFactory"
username="test"
password="test"
host="localhost"
port="27017"
db="test" />
相关问题
- 1. 错误通过JNDI
- 2. 查找通过JNDI名称
- 3. 通过JNDI使用ActiveMQ
- 4. 通过JNDI实例化ActiveMQSslConnectionFactory
- 5. MongoDB的 - 通过
- 6. 通过JNDI访问/ ex实体类
- 7. 获取春JDBC通过JNDI JDBC连接
- 8. 如何通过JNDI更改LDAP密码
- 9. 通过JNDI查找消息驱动Bean
- 10. Spring-hibernate:通过tomcat的dataSource jndi
- 11. 通过JNDI更新LDAP加密密码
- 12. Tomcat:通过JNDI使用FTP连接
- 13. 如何通过JNDI检索LDAP密码
- 14. 无法通过JNDI查找OSGI服务
- 15. 通过REST或通过JNDI消费服务
- 16. 的MongoDB:$ $通过在
- 17. Filtter通过MongoDB中
- 18. MongoDB中通过计数
- 19. MongoDB的 - 通过重叠率
- 20. UPSERT通过索引MongoDB中
- 21. MongoDb通过参数删除
- 22. 通过Mesos发送给MongoDB
- 23. 通过Node.js插入MongoDB
- 24. MongoDB的:通过使用
- 25. 主义的MongoDB通过ID
- 26. 组通过在mongoid/mongodb的
- 27. MongoDB的 - 通过列表
- 28. Mongodb不通过express.js保存
- 29. MongoDB通过Java选择
- 30. 通过迭代在MongoDB中