使用外部Tomcat 9服务器使用JNDI配置数据源:Spring Boot [英] Configure DataSource Using JNDI Using external Tomcat 9 Server: Spring Boot

查看:127
本文介绍了使用外部Tomcat 9服务器使用JNDI配置数据源:Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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并对其进行测试。

这篇关于使用外部Tomcat 9服务器使用JNDI配置数据源:Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆