如何使用外化属性文件在Tomcat中执行JNDI

时间:2015-05-18 16:09:16

标签: java spring tomcat jndi

我只是使用外部化为JNDI配置tomcat。我做错了一些事,但没有任何线索。

在Context.xml中

<Resource name="jdbc/AppDB" auth="Container" type="javax.sql.DataSource"
                            driverClassName="org.postgresql.Driver"
                            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
                            url="${app.env.database.url}"
                            username="${app.env.database.user}"
                            password="${app.env.database.password}"
                            maxActive="100"
                            maxIdle="50"
                            minIdle="10"
                            suspectTimeout="60"
                            timeBetweenEvictionRunsMillis="30000"
                            minEvictableIdleTimeMillis="60000"
                            validationQuery="select 1"
                            validationInterval="30000"
                            testOnBorrow="true"
                            removeAbandoned="true"
                            removeAbandonedTimeout="60"
                            abandonWhenPercentageFull="10"
                            maxWait="10000"
                            jdbcInterceptors="ResetAbandonedTimer;StatementFinalizer"
            />

在运行Tomcat时,我正在设置此

-DappProperties=c:/user/abc/app.properties

在启动服务器时,我遇到错误。

Caused by: javax.naming.NamingException: Driver:org.postgresql.Driver@d8cf794 returned null for URL:${app.env.database.url}
    at org.apache.naming.NamingContext.lookup(NamingContext.java:858)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
    ... 31 more

任何人都可以帮忙。 请注意:如果我使用实际值更改$ {app.env。},应用程序将正常运行。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

你在使用Maven吗?您需要使用资源过滤插件,以便maven将您的属性文件值转换为实际值。类似的东西:

<build>
...
<resources>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
  ...
</resources>
...

目录中包含xml文件应该可以工作