java.sql.SQLException:驱动程序:com.mysql.jdbc.Driver@54d29105为URL返回null:$ {db.url}

时间:2016-09-26 11:42:35

标签: java maven spring-boot maven-profiles spring-profiles

我正在使用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

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在(可能)开发构建的个人资料try { String s = s.substring(7,14); } catch (StringIndexOutOfBoundsException e) { //somehow process the situation in which the string is too short. } 中,您可以定义profile-development。但错误似乎没有解决表达式db.url

最可能的错误是,未使用正确的配置文件进行构建,或者实际上其他(默认)配置文件错过了${db.url}