带嵌入式Tomcat的Jndi Mongodb Spring启动,

时间:2018-02-28 13:29:01

标签: java mongodb spring-boot

使用嵌入式Tomcat的Jndi Mongodb Spring启动,

无法加载JDBC驱动程序类'mongodb.jdbc.MongoDriver'。相反,我已经在依赖项中添加了mongodb-java-driver?

任何帮助

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {
            ContextResource mongoResource = new ContextResource();
            mongoResource.setName("jdbc/Mongodb");
            mongoResource.setType(DataSource.class.getName());
            mongoResource.setProperty("driverClassName", "mongodb.jdbc.MongoDriver");
            mongoResource.setProperty("url", "jdbc:mongo://localhost:27017/skywriter");
            mongoResource.setProperty("username", "skywriter");
            mongoResource.setProperty("password", "skywriter");
            context.getNamingResources().addResource(mongoResource);

        }

    };
}

@Bean(destroyMethod = "")
@Primary
public DataSource mongoJNDIDataSource() throws IllegalArgumentException, NamingException {
    JndiObjectFactoryBean mongodb = new JndiObjectFactoryBean();
    mongodb.setJndiName("java:comp/env/jdbc/Mongodb");
    mongodb.setProxyInterface(DataSource.class);
    mongodb.setLookupOnStartup(false);
    mongodb.afterPropertiesSet();
    return (DataSource) mongodb.getObject();
}

1 个答案:

答案 0 :(得分:0)

您需要包含保存JDBC驱动程序的JAR。您使用的是哪家供应商?它不属于官方mongodb-java-driver