[英]Configuring Spring-Boot Autowired JdbcTemplate with Derby
我已將gs-relational-data-access-complete的副本克隆到我的工作區,下載了Apache Derby的獨立副本並將其啟動為“./bin/startNetworkServer”。
我想指出我的Spring Boot項目使用Apache Derby的外部實例,而不是Spring Boot使用的默認嵌入式H2數據庫。 為了節省時間,我想使用Spring Boot @Autowired注釋。
當我發布Derby的新副本時,沒有問題:
./bin/startNetworkServer
11月21日星期六11:11:06 GMT 2015:使用基本服務器安全策略安裝安全管理器。
11月21日星期六11:11:06 GMT 2015:Apache Derby網絡服務器 - 10.12.1.1 - (1704137)啟動並准備接受端口1527上的連接
當我啟動Spring Boot應用程序時出現問題:
2015-11-21 11:55:07.312 ERROR 11361 --- [main] oatomcat.jdbc.pool.ConnectionPool:無法創建池的初始連接。
java.sql.SQLException:驅動程序:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc為URL返回null:jdbc:derby://10.12.1.1:1527 / example1; create = true; user = test1; password = pass1
在org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326)〜[tomcat-jdbc-8.0.28.jar:na]
如何在仍然利用Spring Boot @Autowired注釋的同時解決此問題的任何幫助將不勝感激。
我的工作區如下:
pom.xml
src/main/java/hello
----Application.java
----Customer.java
src/main/resources
----application.properties
pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties:
spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
spring.datasource.username=test1
spring.datasource.password=pass1
Application.java:
package hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class, args);
}
@Autowired
JdbcTemplate jdbcTemplate;
這是不匹配的:
Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null
for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
您指定使用Derby 嵌入式驅動程序,但您已為Derby提供了數據庫的客戶端 - 服務器 URL。
Derby檢測到不匹配並拒絕您的連接嘗試。
如果要使用客戶端 - 服務器 URL,則需要使用Derby ClientDriver類,並且需要在CLASSPATH中使用derbyclient.jar。
如果要使用嵌入式驅動程序,則需要使用嵌入式驅動程序URL,例如
jdbc:derby:example1;create=true;user=test1;password=pass1
更改
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
通過
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.13.1.1</version>
</dependency>
並在配置中:
spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true
spring.datasource.username=test1
spring.datasource.password=pass1
spring.datasource.driver-class-name=org.apache.derby.jdbc.ClientDriver
問題是derby依賴是針對嵌入式數據庫的,需要客戶端依賴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.