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

How to do JNDI in Tomcat by using externalized Property file

我只是为使用外部化的 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.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.},应用程序运行良好。

感谢您的帮助

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

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

包含您的 xml 文件的目录应该有效