2010-11-02 12 views
5

के माध्यम से क्या आप जानते हैं कि वसंत में मोंडोडब उदाहरण स्थापित करना संभव है जैसे किसी अन्य डीबी की तरह jndi से डेटा स्रोत के माध्यम से?MongoDb jndi

Thx

उत्तर

0

आप JDBC का उपयोग के साथ नियमित रूप से आरडीबीएमएस की तरह मतलब है, तो जवाब नहीं है।

2

ऐसा करने के लिए, आपको MongoDB के लिए एक जेडीबीसी ड्राइवर इत्यादि की आवश्यकता होगी। मुझे केवल एक मिला है, और इसे मोंगोडीबी पृष्ठ से "प्रयोगात्मक" कहा जाता है: GitHub JDBC Driver for MongoDB

इस सीमा को काम करने के लिए, आप कुछ स्प्रिंग बीन्स सेट कर सकते हैं और अपने आवेदन डीएओ के लिए एक मोंगोडीबी कार्यान्वयन बना सकते हैं (इस तरह, आपको डीएओ इंटरफ़ेस और उसके क्लाइंट घटकों को बदलने की आवश्यकता नहीं होगी)।

इस लेख में मदद कर सकते हैं:

11

हाँ, यह संभव है, क्यों किसी में elses कोड जब आप अपने खुद के JNDI कारखाना बना सकते हैं भरोसा? बस एक वर्ग बनाएं जो javax.naming.spi.ObjectFactory और एक बीन लागू करता है जो JNDI संदर्भ से मोंगो खींचता है, मैंने वसंत डेटा-मोंगो मोंगो टेम्पलेट ऑब्जेक्ट के लिए इसे कॉन्फ़िगर किया।

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); 
    } 
} 

}

वसंत सेम:

@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

मोंगोडीबी के लिए जेडीबीसी चालक प्रत्यारोपण प्रदान करने का एक और प्रयास है। यहाँ:

https://sourceforge.net/projects/mongojdbcdriver

किसी भी उपाय से पूरा नहीं है, लेकिन उम्मीद है कि एक JDBC कार्यान्वयन है कि जल्द ही जावा डेवलपर्स के लिए परिचित है प्रदान करेगा।

3

ObjectFactory इंटरफेस (CustomMongoJNDIFactory) के जुआन मेलो के कस्टम कार्यान्वयन पुनर्प्रयोग, यह भी context.xml file में वसंत के jee नाम स्थान का JNDI-देखने टैग का उपयोग और बिलाव config इसी के लिए कॉन्फ़िगर किया जा सकता है, इस तरह:

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" />