Spring boot datasource configuration using tomcat

In this post, we will see Spring boot datasource configuration example using an external tomcat server. We are going to use Tomcat 9 and spring boot 2.x for this tutorial. We will have application.properties file where we will provide database details. We need to exclude embedded tomcat so that we can further deploy our war file in external tomcat.

For datasource creation we need to define the application.properties file and configuration class with the following details.

application.properties file.

# Connection url for the database
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=SYSTEM
spring.datasource.password=oracle1
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
# Show or not log for each sql query
spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto =update
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect

#server.port = 9091

 

 

AppConfiguration.java

@Configuration
@EnableJpaRepositories(basePackages = “com.springboot.repository”)
@EntityScan(“com.springboot.entity”)
@PropertySource({(“classpath:application.properties”) })
public class AppConfiguration {

@Autowired
private Environment environment;

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
String driverClassName = environment.getProperty(“spring.datasource.driver-class-name”);
String url = environment.getProperty(“spring.datasource.url”);
String username = environment.getProperty(“spring.datasource.username”);
String password = environment.getProperty(“spring.datasource.password”);
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}

}

Here we will create two REST APIs, first one will save the data in the database and the second one to retrieve the data as below.

http://localhost:8181/springbootdeployment/book/savebook
http://localhost:8181/springbootdeployment/book/getbook/{bookId}

 

Spring boot datasource configuration using tomcat

 

http://localhost:8181/springbootdeployment/book/getbook/{bookId}

 

 

Let’s see Spring boot Datasource configuration using tomcat example using Oracle.

prerequisites.

  • JDK 1.8
  • Oracle 10g
  • Eclipse
  • Maven
  • Postman
  • Tomcat 9

Step 1 – open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next.

Step 2 – Fill all details as below and click on finish.

Since I have already project with name springbootdeployment, it’s showing an error.

Step 3 – You will see below directory structure.

 

Step 4 – modify the pom.xml and let maven download the jar.

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK. See line number 16.

If you see any error for oracle dependency then follow these steps.

Let maven download all necessary jar. Once it is done we will able to see maven dependency folder which contains different jar files.

We are good to go. We can start writing our controller classes, ServiceImpl and Repository. The folder structure of the application.

 

Step 5 – Define main class SpringMain.java and other classes and interfaces.

Book.java

BookService.java

BookRepsitory.java

BookServiceImpl.java

BookController.java

AppConfiguration.java

And finally, we have application.properties.

Let’s build the application. Right-click on application(i.e spingbootdeployment) and run as maven clean then run as maven install.

Spring boot datasource configuration using tomcat

 

After running maven install, in target folder war file should be there with name spingbootdeployment.war.

 

Spring boot datasource configuration using tomcat

 

Let’s download tomcat9 and extract it somewhere in the local drive and configure to eclipse.

Spring boot datasource configuration using tomcat

Right-click on the application and click on Run as a server.

Spring boot datasource configuration using tomcat

 

 

If you get any port-related issue change the port in the server as below.

Double click on the configured server and change the HTTP/1.1 port

Our application should be deployed now.

 

Let’s test the rest APIs.

First, we will save the book record. Don’t forget we are not going to create table let’s hibernate take care of it.

http://localhost:8181/springbootdeployment/book/savebook

Spring boot datasource configuration using tomcat

 

http://localhost:8181/springbootdeployment/book/getbook/{bookId}

That’s all about Spring boot Datasource configuration using tomcat. Any query feel free to leave a comment.

Apache Tomcat docs.

Deploy Spring boot war in JBoss EAP server.
Jboss 7 EPA datasource configuration using oracle and spring boot.
Deploy Spring Boot application on external Tomcat.
Deploy multiple war files in JBoss to different port.

Hibernate/JPA tutorial.

Spring Data JPA tutorial.

 

Top