我的环境是Centos 7,Tomcat 8和Java 8我开发了spring-boot应用程序。我想在application.properties中外设参数,所以我将它们复制到外部属性文件中。
这里,application.properties文件(spring-boot app):
spring.datasource.username= ${db.username}
spring.datasource.password= ${db.password}
这是我的外部属性文件:
内容:
db.username=root
db.password=test
我在tomcat / bin文件夹中创建了setenv.sh文件来添加java选项这是我的setenv.sh:
内容:
JAVA_OPTS="$JAVA_OPTS -Dspring.config.additional-location=/home/centos/myjarvia/configuration/application.properties"
但是当tomcat启动时它会失败。(即使catalina.out日志文件显示找到属性文件...
catalina.out文件内容:
26-Mar-2019 22:17:04.037 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.39
26-Mar-2019 22:17:04.039 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 14 2019 11:24:26 UTC
26-Mar-2019 22:17:04.039 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.39.0
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-957.5.1.el7.x86_64
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_201-b09
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /opt/tomcat
26-Mar-2019 22:17:04.040 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /opt/tomcat
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dspring.config.additional-location=/home/centos/myjarvia/configuration/application.properties
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/tomcat
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/tomcat
26-Mar-2019 22:17:04.041 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/tomcat/temp
26-Mar-2019 22:17:04.042 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environm$
26-Mar-2019 22:17:04.147 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
26-Mar-2019 22:17:04.167 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Mar-2019 22:17:04.200 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
26-Mar-2019 22:17:04.202 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
26-Mar-2019 22:17:04.203 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 584 ms
26-Mar-2019 22:17:04.242 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
26-Mar-2019 22:17:04.242 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.39
26-Mar-2019 22:17:04.282 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/tomcat/webapps/MyJarviaBackDevice-1.0.war]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2019-03-26 22:17:08.449 INFO 30878 --- [ost-startStop-1] c.t.mc.myjarvia.MyJarviaBackDeviceApp : Starting MyJarviaBackDeviceApp v0.0.1 on serveur-1 with PID 30878 (/opt/tomcat/webapps/MyJarvia$
2019-03-26 22:17:08.468 INFO 30878 --- [ost-startStop-1] c.t.mc.myjarvia.MyJarviaBackDeviceApp : No active profile set, falling back to default profiles: default
2019-03-26 22:17:08.558 INFO 30878 --- [ost-startStop-1] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplica$
2019-03-26 22:17:09.926 INFO 30878 --- [ost-startStop-1] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [context/dataBinder_context.xml]
2019-03-26 22:17:11.916 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of ty$
2019-03-26 22:17:11.988 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfigura$
2019-03-26 22:17:12.001 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuratio$
2019-03-26 22:17:12.002 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHand$
2019-03-26 22:17:12.021 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.method.configuration.Jsr250MetadataSourceC$
2019-03-26 22:17:12.033 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'jsr250MethodSecurityMetadataSource' of type [org.springframework.security.access.annotati$
2019-03-26 22:17:12.036 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityC$
2019-03-26 22:17:12.056 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.Delegat$
2019-03-26 22:17:12.065 INFO 30878 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boo$
2019-03-26 22:17:12.110 INFO 30878 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3552 ms
2019-03-26 22:17:13.138 INFO 30878 --- [ost-startStop-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-03-26 22:17:14.389 ERROR 30878 --- [ost-startStop-1] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLException: Access denied for user '${db.username}'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1714) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1224) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2199) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2230) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2025) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:778) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.47.jar:5.1.47]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201]
我不明白出了什么问题
只需在外部属性文件中添加它
spring.datasource.username =提及数据库用户名
spring.datasource.password =提及数据库密码
它将无意义地覆盖
并删除像db.username这样的其他东西