Java 将Spring引导示例JNDI转换为在单独的Tomcat实例中工作

Java 将Spring引导示例JNDI转换为在单独的Tomcat实例中工作,java,tomcat,spring-boot,jndi,Java,Tomcat,Spring Boot,Jndi,我在Github上做了git克隆,在嵌入式模式下运行,一切正常 所以,现在我想将其转换为在单独的Tomcat实例上运行 首先,我将pom.xml打包更改为war 其次,我更改了sampletomcatjndiplication类以扩展SpringBootServletInitializeradd添加了以下方法: @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application)

我在Github上做了
git克隆
,在嵌入式模式下运行,一切正常

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

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

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

@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" />
在现有Tomcat实例上运行时,我总是遇到以下异常:

名称[jdbc/finance]在此上下文中不受约束。找不到[jdbc]

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


如何让它在现有的Tomcat上工作?

问题根本不在我的应用程序中,而是在我的Tomcat配置中。由于接受了的答案,我发现我需要向Tomcat的context.xml添加ResourceLink元素。我想知道为什么Tomcat JNDI HowTo没有提到它

@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();
}