简体   繁体   English

嵌入式tomcat jndi定义

[英]Embedded tomcat jndi definition

I'm using spring with embedded tomcat. 我正在使用带有嵌入式tomcat的spring。 I don't want application.properties to be used anymore, since it should be published to a webserver running tomcat and there I'm now using a JNDI dataSource from a context.xml which works very well. 我不希望再使用application.properties ,因为应该将它发布到运行tomcat的Web服务器上,现在我正在使用来自context.xml的JNDI数据源,效果很好。 Now I want to define that JNDI resource for embedded tomcat, but it does not work. 现在,我想为嵌入式tomcat定义该JNDI资源,但是它不起作用。

What I've tried: 我尝试过的

context.xml context.xml

<?xml version='1.0' encoding='utf-8'?>

<Context>

    <Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/db"/>

</Context>

Configuration.java 配置文件

@Bean
public DataSource dataSource() {
    final JndiDataSourceLookup lookup = new JndiDataSourceLookup();
    lookup.setResourceRef(true);
    return lookup.getDataSource("jdbc/db");
}

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

and I'm getting: 我得到:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]:
Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'jdbc/db';
nested exception is javax.naming.NameNotFoundException: Name [jdbc/db] is not bound in this Context. Unable to find [jdbc].

Please help me, thanks :) 请帮助我,谢谢:)

Try with 试试看

lookup.getDataSource("java:/comp/env/jdbc/db");

without java:comp, jndi can not be found."comp" is short for component and is bound to root context. 如果没有java:comp,就找不到jndi。“ comp”是组件的缩写,绑定到根上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM