gpt4 book ai didi

java - 将 Spring Boot 示例 JNDI 转换为在单独的 Tomcat 实例中工作

转载 作者:行者123 更新时间:2023-11-28 23:29:22 24 4
gpt4 key购买 nike

<分区>

我在 Spring Boot Sample JNDI app 上做了 git clone在 Github 上,以嵌入式模式运行它,它工作正常。

所以,现在我想将其转换为在单独的 Tomcat 实例上运行。

首先,我将 pom.xml 打包更改为 war

其次,我更改了 SampleTomcatJndiApplication 类以扩展 SpringBootServletInitializer 添加了以下方法:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleTomcatJndiApplication.class);
}

我还更改了 JNDI 属性。 Tomcat 实例 (Tomcat 8.0.24) 在 server.xml 中定义了以下上下文:

    <Resource auth="Container" 
driverClassName="com.mysql.jdbc.Driver"
maxActive="8" maxIdle="4"
name="jdbc/finance" type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/finance"
username="xxxx" password="yyyy" />

我的 DataSource bean 定义为

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

在现有的 Tomcat 实例上运行时,我总是遇到此异常:

名称 [jdbc/finance] 未绑定(bind)在此上下文中。无法找到 [jdbc]。

在嵌入式模式下运行时,它工作正常。

我怎样才能让它在现有的 Tomcat 上运行?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com