Reputation: 5733
I've set up spring.datasource.* in application.properties:
spring.datasource.url=jdbc:h2:./data/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Then I configured JdbcTemplate Bean
@Bean
@Autowired
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
But when I start application, I see in console
Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
Instead of my settings. Why?
Upvotes: 4
Views: 15148
Reputation: 23
I had the same problem using spring boot 3.4.1 and fixed it by adding spring.test.database.replace=none in application.properties
Upvotes: 0
Reputation: 21
I had the same issue and I hope it helps you.
The problem seems to be:
When you login into H2 console, it writes metadata in your user folder (.h2.server.properties). This file just configures what will appear in the console UI.
Pay close attention to the URL you put in the JDBC URL
field of the console UI. It must match with the URL you define in the spring.datasource.url
of the application.properties
file.
The trick is: when running just an example application, it DO NOT create a data file until you connect/commit something into the database. So, you think the autoconfigure feature
is not doing the job.
But when you first modify the database state, H2 creates the data file something.mv.db
in the right folder.
Assuming you just created a new application with the following dependencies (i.e. JDBC
,H2
and web
), try the following to see my point:
1) set up your pom.xml (it is the default for these three dependencies I mentioned):
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2) in the application.properties file:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:file:./data/test
3) Open a file explorer window to show the contents of the root folder of your project (the same folder of your pom.xml
).
4) Then, go to <server>:8080/h2-console
.
In the JDBC URL
field of the console UI, type: jdbc:h2:file:./data/test
and connect.
5) You will see the 'data' folder being created and inside it, the data file test.mv.db
in this case.
Upvotes: 2
Reputation: 4156
If you want to inject JdbcTemplate
in your controller class,
private final JdbcTemplate jdbcTemplate;
@Autowired
public MyController(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
In this way you can autowire jdbcTemplate
in any spring bean.
Add spring boot jdbc dependency in your pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
See this spring boot documentation for more information.
Upvotes: 0
Reputation: 5733
It's always a magic.
When the problem occured I have had these 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>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
But when I changed it into
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
The problem gone...
Upvotes: 8
Reputation: 3074
Solution to your question
Sample Snippet below.
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
@ImportResource({ "classpath:datasource-config.xml"})
public class Sample {
In datasource-config.xml you can use your datasource properties file as well.
Enjoy :-)
Upvotes: 2