Java 如何使用外部化属性文件在Tomcat中进行JNDI
我只是使用外部化的方法为JNDI配置tomcat。我做错了一些事,但毫无头绪 在Context.xml中Java 如何使用外部化属性文件在Tomcat中进行JNDI,java,spring,tomcat,jndi,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.tomca
<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"
/>
在启动服务器时,我发现以下错误
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.},应用程序运行良好
谢谢您的帮助您正在使用Maven吗?您需要使用资源过滤插件,以便maven将属性文件值转换为实际值。比如:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
...
</resources>
...
...
src/main/resources
...
...
包含xml文件的目录应该在哪里工作请参阅@Sasikumar Murugesan-这并不能解决我的问题。如果你仔细看看我的问题,conext.xml有${app.env.url}等,应该通过Startup上的属性文件注入。我不知道Tomcat中有任何这样的功能,在文档中也找不到。你们有并没有一个你们读到这应该是可能的来源?我认为Maven是一个构建工具,我正在寻找的是运行时赋值。如何通过mavenHere设置tomcat/conf/server.xml属性文件还有一个类似的问题,可能会对您有所帮助:我的观点是,您需要使用某种构建工具将模板变量转换为实际变量。Maven可以帮你。你是对的,我们可以这样做,但我需要这个用于生产。我不能使用Maven tomcat插件进行生产。我不认为没有一些工具你就可以做到这一点。
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
...
</resources>
...