【发布时间】:2016-09-26 11:42:34
【问题描述】:
我正在使用 Spring Boot 和项目配置文件。我在我的 maven 配置 (pom.xml) 中添加了一些配置文件,我尝试在 application.propertie 中导入配置文件属性,但出现异常。
这是我的 pom.xml 的一部分:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.x.server.MainApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile-development</id>
<properties>
<environment>dev</environment>
<db.username>me</db.username>
<db.password>testme</db.password>
<db.url>jdbc:mysql://localhost:3306/testdb</db.url>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>environment</name>
<value>dev</value>
</property>
</activation>
</profile>
应用程序属性:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=${db.url}
spring.datasource.username=${db.username}
spring.datasource.password=${db.password}
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
但我得到了这个例外:
Caused by: java.sql.SQLException: Driver:com.mysql.jdbc.Driver@5d1759fb returned null for URL:${db.url}
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:141)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 45 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
有人有什么想法吗?
【问题讨论】:
-
你是如何配置 bean 的?你能展示那堂课吗?我认为您缺少 PropertySource。
-
我不适用于 bean,但我在主类中使用 @EnableAutoConfiguration。
-
好的。但是您的
${db.url}和其他类似属性在哪里定义? -
我假设与@DeendayalGarg 相同 - 在您的 application.properties 文件中是以下属性
spring.datasource.url=${db.url},这不是有效的 url。 -
我在 pom.xml 中定义了 db.url。检查配置文件环境。
jdbc:mysql://localhost:3306/testdb
标签: java maven spring-boot maven-profiles spring-profiles