【问题标题】:MongoDb via jndiMongoDb 通过 jndi
【发布时间】:2011-05-03 20:08:23
【问题描述】:

您知道是否可以像其他任何数据库一样通过来自 jndi 的数据源在 spring 中设置 mongodb 实例?

谢谢

【问题讨论】:

    标签: java mongodb jndi nosql


    【解决方案1】:

    是的,有可能,当您可以创建自己的 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);
        }
    }
    

    }

    春豆:

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

    上下文.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>
    

    【讨论】:

      【解决方案2】:

      重用Juan Melo自定义实现的ObjectFactory接口(@98​​7654322@),也可以使用Spring的jee命名空间的jndi-lookup标签和context.xml file中对应的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" />
      

      【讨论】:

        【解决方案3】:

        为此,您需要一个用于 MongoDB 的 JDBC 驱动程序 impl。我只找到了一个,它在 MongoDB 页面中被称为“实验性”:GitHub JDBC Driver for MongoDB

        要解决此限制,您可以设置一些 Spring bean 并为您的应用程序 DAO 创建一个 MongoDB 实现(这样,您无需更改 DAO 接口及其客户端组件)。

        这篇文章可能会有所帮助:

        【讨论】:

          【解决方案4】:

          如果您的意思是像具有 JDBC 访问权限的常规 RDBMS,那么答案是否定的。

          【讨论】:

            【解决方案5】:

            还有另一项工作是为 MongoDB 提供 JDBC 驱动程序 impl。这里:

            https://sourceforge.net/projects/mongojdbcdriver

            无论如何都不完整,但有望很快提供 Java 开发人员熟悉的 JDBC 实现。

            【讨论】:

              猜你喜欢
              • 2019-05-25
              • 2019-05-25
              • 2010-10-01
              • 1970-01-01
              • 2016-07-16
              • 1970-01-01
              • 1970-01-01
              • 2012-05-02
              • 2011-05-22
              相关资源
              最近更新 更多