使用外部Tomcat 9服务器使用JNDI配置数据源:Spring Boot

2022-05-18 00:00:00 java tomcat9 spring-boot spring-mvc jndi

我有一个SpringBootApplication,打包为WAR文件:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

在应用程序上。属性:

spring.datasource.jndi-name=java:comp/env/jdbc/bonanza

但在Tomcat 9中部署WAR时,我在日志上看到了这些消息:

Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.

日志:

12:37:53.989 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.datasource.jndi-name]
12:37:53.989 [main] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.datasource.jndi-name] not found - trying original name [spring.datasource.jndi-name]. javax.naming.NameNotFoundException: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].
12:37:53.990 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.datasource.jndi-name]
12:37:53.991 [main] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.datasource.jndi-name] threw NamingException with message: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.
12:37:53.995 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.datasource.jndi-name]
12:37:53.996 [main] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.datasource.jndi-name] not found - trying original name [spring.datasource.jndi-name]. javax.naming.NameNotFoundException: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].
12:37:53.996 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.datasource.jndi-name]
12:37:53.997 [main] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.datasource.jndi-name] threw NamingException with message: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.
12:37:53.998 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.jndi-name' in PropertySource 'configurationProperties' with value of type String

在我的tomcat9/conf/context.xml:

 <Resource  name="jdbc/bonanza" 
                auth="Container" 
                type="javax.sql.DataSource"
                maxTotal="100" 
                maxIdle="30" 
                maxWaitMillis="10000"
                username="a_usr" 
                password="Mu*7gydlcdstg100@" 
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://172.175.77.55:3306/a_db"
        />

解决方案

如错误所示,Spring Boot无法在JNDI查找中找到密钥。在Spring Boot的嵌入式Tomcat中禁用了JNDI,因此需要使用Tomcat#enableNaming启用它,一旦启用,就需要在JNDI中创建一个查找条目。您可以参考我从一个Spring Boot项目维护人员存储库GitHub repo JNDI-Tomcat

复制的以下代码
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

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

        @Override
        protected void postProcessContext(Context context) {
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/bonanza");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", "your.db.Driver");
            resource.setProperty("url", "jdbc:yourDb");

            context.getNamingResources().addResource(resource);
        }
    };
}

[编辑]

由于您没有使用嵌入式Tomcat服务器,因此您可以使用Tomcat配置文件来配置JNDI:

在server.xml中,在<GlobalNamingResources>下创建资源

<Resource auth="Container" driverClassName="..." 
                           maxActive="..." 
                           maxIdle="..." 
                           maxWait="..." 
                           name="jdbc/bonanza"  
                           username="..."
                           password="..."
                           type="..."
                           url="..."/>

在Conext.xml中,您可以链接资源

<context>
    <ResourceLink auth="Container" name="jdbc/bonanza" global="jdbc/bonanza" type="javax.sql.DataSource" />
</context>
此外,请确保您没有使用Spring-Bootmain方法启动应用程序。您需要使用maven/Gradle构建WAR文件,然后将其部署到Tomcat并对其进行测试。

相关文章