Jndi Mongodb Spring引导嵌入式Tomcat,[重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

带嵌入式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();
}
java mongodb spring-boot
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.