{"id":22694,"date":"2023-11-21T07:09:02","date_gmt":"2023-11-21T00:09:02","guid":{"rendered":"https:\/\/huongdanjava.com\/?p=22694"},"modified":"2023-11-21T07:09:02","modified_gmt":"2023-11-21T00:09:02","slug":"multiple-datasources-in-spring-boot-application","status":"publish","type":"post","link":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html","title":{"rendered":"Multiple Datasources in Spring Boot application"},"content":{"rendered":"<p>For Spring Boot applications that only work with one database, you can refer to the article <a href=\"https:\/\/huongdanjava.com\/configure-database-in-spring-boot-project-using-jpa-starter-dependency.html\" target=\"_blank\" rel=\"noopener\">Configure database in Spring Boot project using JPA Starter dependency<\/a> to know how to configure the database for the application. In reality, sometimes we will work with applications that need to connect to 2 or more different databases. So in these cases, how can we connect to all these databases to work with them? I will guide you on how to do this in this tutorial!<\/p>\n<p>First, I will create a new Spring Boot project with Spring Data JPA Starter, MySQL Driver, PostgreSQL Driver, and Lombok as an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22696 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/11\/multiple-datasources-in-spring-boot-application-1.png\" alt=\"\" width=\"700\" height=\"771\" \/><\/p>\n<p>MySQL Driver and PostgreSQL Driver will help my example application connect to the MySQL database and PostgreSQL database as an example!<\/p>\n<p>As an example and for simplicity, I will define a table in MySQL database containing student information with 2 columns as follows:<\/p>\n<pre class=\"lang:mysql decode:true \">CREATE TABLE `student` (\r\n  `id` bigint(20) NOT NULL AUTO_INCREMENT,\r\n  `name` varchar(50) DEFAULT NULL,\r\n  PRIMARY KEY (`id`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;<\/pre>\n<p>Entity of table &#8220;student&#8221; is as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.mysql.entity;\r\n\r\nimport jakarta.persistence.Column;\r\nimport jakarta.persistence.Entity;\r\nimport jakarta.persistence.GeneratedValue;\r\nimport jakarta.persistence.Id;\r\nimport jakarta.persistence.Table;\r\nimport java.io.Serializable;\r\nimport lombok.Getter;\r\nimport lombok.Setter;\r\n\r\n@Table(name = \"student\")\r\n@Entity\r\n@Getter\r\n@Setter\r\npublic class Student implements Serializable {\r\n\r\n  private static final long serialVersionUID = 1L;\r\n\r\n  @Id\r\n  @GeneratedValue\r\n  private Long id;\r\n\r\n  @Column\r\n  private String name;\r\n}<\/pre>\n<p>The Repository class for this Student entity is as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.mysql.repository;\r\n\r\nimport com.huongdanjava.springboot.mysql.entity.Student;\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\n\r\npublic interface StudentRepository extends JpaRepository&lt;Student, Long&gt; {\r\n\r\n}<\/pre>\n<p>Similarly, I also defined a table in the PostgreSQL database containing class information with 2 columns as follows:<\/p>\n<pre class=\"lang:pgsql decode:true \">CREATE TABLE clazz (\r\n  id serial PRIMARY KEY,\r\n  name varchar(50) NOT NULL\r\n)<\/pre>\n<p>Entity of table &#8220;clazz&#8221; will be as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.postgresql.entity;\r\n\r\nimport jakarta.persistence.Column;\r\nimport jakarta.persistence.Entity;\r\nimport jakarta.persistence.GeneratedValue;\r\nimport jakarta.persistence.Id;\r\nimport jakarta.persistence.Table;\r\nimport java.io.Serializable;\r\nimport lombok.Getter;\r\nimport lombok.Setter;\r\n\r\n@Table(name = \"clazz\")\r\n@Entity\r\n@Getter\r\n@Setter\r\npublic class Clazz implements Serializable {\r\n\r\n  private static final long serialVersionUID = 1L;\r\n\r\n  @Id\r\n  @GeneratedValue\r\n  private Long id;\r\n\r\n  @Column\r\n  private String name;\r\n}<\/pre>\n<p>The repository class for the Clazz entity is as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.postgresql.repository;\r\n\r\nimport com.huongdanjava.springboot.postgresql.entity.Clazz;\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\n\r\npublic interface ClazzRepository extends JpaRepository&lt;Clazz, Long&gt; {\r\n\r\n}<\/pre>\n<p>Now, I will configure the information of these two databases.<\/p>\n<p><strong>I will create a new PostgreSQLDatabaseConfiguration class to configure the PostgreSQL database<\/strong> with <a href=\"https:\/\/huongdanjava.com\/configure-spring-data-jpa-with-enablejparepositories-annotation.html\" target=\"_blank\" rel=\"noopener\">the @EnableJpaRepositories annotation<\/a> as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.postgresql;\r\n\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.data.jpa.repository.config.EnableJpaRepositories;\r\n\r\n@Configuration\r\n@EnableJpaRepositories(\r\n    basePackages = {\"com.huongdanjava.springboot.postgresql.repository\"},\r\n    entityManagerFactoryRef = \"postgresqlEntityManagerFactory\",\r\n    transactionManagerRef = \"postgresqlTransactionManager\"\r\n)\r\npublic class PostgreSQLDatabaseConfiguration {\r\n  \r\n}\r\n<\/pre>\n<p>The basePackages attribute will have the value of the package containing all repositories related to the PostgreSQL database, mine here is &#8220;com.huongdanjava.springboot.postgresql.repository&#8221;, guys! The values of the two attributes entityManagerFactoryRef and transactionManagerRef will point to the two beans that I will define for the PostgreSQL database as follows:<\/p>\n<pre class=\"lang:java decode:true \">@Bean\r\n@Primary\r\n@ConfigurationProperties(prefix = \"postgresql.datasource\")\r\npublic DataSource postgresqlDataSource() {\r\n  return DataSourceBuilder.create().build();\r\n}\r\n\r\n@Bean\r\npublic LocalContainerEntityManagerFactoryBean postgresqlEntityManagerFactory(\r\n    EntityManagerFactoryBuilder builder) {\r\n  return builder\r\n      .dataSource(postgresqlDataSource())\r\n      .packages(\"com.huongdanjava.springboot.postgresql.entity\")\r\n      .build();\r\n}\r\n\r\n@Bean\r\npublic PlatformTransactionManager postgresqlTransactionManager(\r\n    EntityManagerFactory postgresqlEntityManagerFactory) {\r\n  return new JpaTransactionManager(postgresqlEntityManagerFactory);\r\n}<\/pre>\n<p>Due to some reasons related to auto-configuration, the EntityManagerFactoryBuilder class in the JpaBaseConfiguration class needs to determine the bean of the Datasource class to initialize itself in the Spring container. You need to specify one of the 2 Datasource beans for their 2 databases as primary by using Spring&#8217;s @Primary annotation, as I did above.<\/p>\n<p>Information related to the PostgreSQL database I will configure in the application.properties file as follows:<\/p>\n<pre class=\"lang:java decode:true \">postgresql.datasource.jdbc-url=jdbc:postgresql:\/\/localhost:5432\/example\r\npostgresql.datasource.driver-class-name=org.postgresql.Driver\r\npostgresql.datasource.username=postgres\r\npostgresql.datasource.password=123456<\/pre>\n<p>Please note that we need to declare postgresql.datasource.jdbc-url instead of postgresql.datasource.url because by default Spring Boot will initialize HirakiDataSource. This HirakiDataSource class uses the HikariConfig class to store database configuration information. This HikariConfig class uses the jdbcUrl attribute to store information about the connection string!<\/p>\n<p><strong>Similar to PostgreSQL, for MySQL database, I will also create a new MySQLDatabaseConfiguration class<\/strong> to configure it as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot.mysql;\r\n\r\nimport jakarta.persistence.EntityManagerFactory;\r\nimport javax.sql.DataSource;\r\nimport org.springframework.boot.context.properties.ConfigurationProperties;\r\nimport org.springframework.boot.jdbc.DataSourceBuilder;\r\nimport org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.data.jpa.repository.config.EnableJpaRepositories;\r\nimport org.springframework.orm.jpa.JpaTransactionManager;\r\nimport org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;\r\nimport org.springframework.transaction.PlatformTransactionManager;\r\n\r\n@Configuration\r\n@EnableJpaRepositories(\r\n    basePackages = {\"com.huongdanjava.springboot.mysql.repository\"},\r\n    entityManagerFactoryRef = \"mysqlEntityManagerFactory\",\r\n    transactionManagerRef = \"mysqlTransactionManager\"\r\n)\r\npublic class MySQLDatabaseConfiguration {\r\n\r\n  @Bean\r\n  @ConfigurationProperties(prefix = \"mysql.datasource\")\r\n  public DataSource mysqlDataSource() {\r\n    return DataSourceBuilder.create().build();\r\n  }\r\n\r\n  @Bean\r\n  public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(\r\n      EntityManagerFactoryBuilder builder) {\r\n    return builder\r\n        .dataSource(mysqlDataSource())\r\n        .packages(\"com.huongdanjava.springboot.mysql.entity\")\r\n        .build();\r\n  }\r\n\r\n  @Bean\r\n  public PlatformTransactionManager mysqlTransactionManager(\r\n      EntityManagerFactory mysqlEntityManagerFactory) {\r\n    return new JpaTransactionManager(mysqlEntityManagerFactory);\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>Information related to MySQL database, I will also configure in the application.properties file as follows:<\/p>\n<pre class=\"lang:java decode:true \">mysql.datasource.jdbc-url=jdbc:mysql:\/\/localhost:3306\/example\r\nmysql.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\r\nmysql.datasource.username=root\r\nmysql.datasource.password=123456<\/pre>\n<p>Now, I will implement the CommandLineRunner interface for the SpringBootMultipleDatasourcesApplication class to retrieve student and class information from the above two databases as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.springboot;\r\n\r\nimport com.huongdanjava.springboot.mysql.repository.StudentRepository;\r\nimport com.huongdanjava.springboot.postgresql.repository.ClazzRepository;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.CommandLineRunner;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\n\r\n@SpringBootApplication\r\npublic class SpringBootMultipleDatasourcesApplication implements CommandLineRunner {\r\n\r\n  @Autowired\r\n  StudentRepository studentRepository;\r\n\r\n  @Autowired\r\n  ClazzRepository clazzRepository;\r\n\r\n  public static void main(String[] args) {\r\n    SpringApplication.run(SpringBootMultipleDatasourcesApplication.class, args);\r\n  }\r\n\r\n  @Override\r\n  public void run(String... args) throws Exception {\r\n    System.out.println(\"Getting all students from MySQL database ...\");\r\n    studentRepository.findAll().forEach(s -&gt; System.out.println(s.getName()));\r\n\r\n    System.out.println(\"Getting all classes from PostgreSQL database ...\");\r\n    clazzRepository.findAll().forEach(c -&gt; System.out.println(c.getName()));\r\n  }\r\n}<\/pre>\n<p>If my MySQL and PostgreSQL databases have the following information about classes and students:<\/p>\n<p>MySQL:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22697 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/11\/multiple-datasources-in-spring-boot-application-2.png\" alt=\"\" width=\"700\" height=\"245\" \/><\/p>\n<p>PostgreSQL:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22698 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/11\/multiple-datasources-in-spring-boot-application-3.png\" alt=\"\" width=\"650\" height=\"332\" \/><\/p>\n<p>then when you run the example, you will see the following results:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-22699 aligncenter\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2023\/11\/multiple-datasources-in-spring-boot-application-4.png\" alt=\"\" width=\"700\" height=\"577\" \/><\/p>\n<p>So we have successfully configured the Spring Boot application to connect and work with multiple databases at the same time!<\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-right kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;right&quot;,&quot;id&quot;:&quot;22694&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;4&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Multiple Datasources in Spring Boot application&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>For Spring Boot applications that only work with one database, you can refer to the article Configure database in Spring Boot project using JPA Starter dependency to know how to configure the database for the application. In reality, sometimes we will work with applications that&hellip; <a href=\"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1680,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[582],"tags":[],"class_list":["post-22694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot","clearfix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Multiple Datasources in Spring Boot application - Huong Dan Java<\/title>\n<meta name=\"description\" content=\"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Multiple Datasources in Spring Boot application - Huong Dan Java\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html\" \/>\n<meta property=\"og:site_name\" content=\"Huong Dan Java\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-21T00:09:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khanh Nguyen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/KhanhNguyenJ\" \/>\n<meta name=\"twitter:site\" content=\"@KhanhNguyenJ\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khanh Nguyen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html\"},\"author\":{\"name\":\"Khanh Nguyen\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"headline\":\"Multiple Datasources in Spring Boot application\",\"datePublished\":\"2023-11-21T00:09:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html\"},\"wordCount\":509,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/10\\\/spring-boot.png\",\"articleSection\":[\"Spring Boot\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html\",\"name\":\"Multiple Datasources in Spring Boot application - Huong Dan Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/10\\\/spring-boot.png\",\"datePublished\":\"2023-11-21T00:09:02+00:00\",\"description\":\"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#primaryimage\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/10\\\/spring-boot.png\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/10\\\/spring-boot.png\",\"width\":300,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/multiple-datasources-in-spring-boot-application.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/huongdanjava.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Multiple Datasources in Spring Boot application\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/\",\"name\":\"Huong Dan Java\",\"description\":\"Java development tutorials\",\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/huongdanjava.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\",\"name\":\"Khanh Nguyen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"width\":1267,\"height\":1517,\"caption\":\"Khanh Nguyen\"},\"logo\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\"},\"description\":\"I love Java and everything related to Java.\",\"sameAs\":[\"https:\\\/\\\/huongdanjava.com\",\"https:\\\/\\\/www.facebook.com\\\/nhkhanh2406\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/KhanhNguyenJ\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Multiple Datasources in Spring Boot application - Huong Dan Java","description":"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html","og_locale":"en_US","og_type":"article","og_title":"Multiple Datasources in Spring Boot application - Huong Dan Java","og_description":"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.","og_url":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html","og_site_name":"Huong Dan Java","article_publisher":"https:\/\/www.facebook.com\/nhkhanh2406","article_author":"https:\/\/www.facebook.com\/nhkhanh2406","article_published_time":"2023-11-21T00:09:02+00:00","og_image":[{"width":300,"height":300,"url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png","type":"image\/png"}],"author":"Khanh Nguyen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/KhanhNguyenJ","twitter_site":"@KhanhNguyenJ","twitter_misc":{"Written by":"Khanh Nguyen","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#article","isPartOf":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html"},"author":{"name":"Khanh Nguyen","@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"headline":"Multiple Datasources in Spring Boot application","datePublished":"2023-11-21T00:09:02+00:00","mainEntityOfPage":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html"},"wordCount":509,"commentCount":0,"publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"image":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png","articleSection":["Spring Boot"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html","url":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html","name":"Multiple Datasources in Spring Boot application - Huong Dan Java","isPartOf":{"@id":"https:\/\/huongdanjava.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#primaryimage"},"image":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png","datePublished":"2023-11-21T00:09:02+00:00","description":"In this tutorial, I guide you all on how to configure multiple Datasources in a Spring Boot application.","breadcrumb":{"@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#primaryimage","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/10\/spring-boot.png","width":300,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/huongdanjava.com\/multiple-datasources-in-spring-boot-application.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/huongdanjava.com\/"},{"@type":"ListItem","position":2,"name":"Multiple Datasources in Spring Boot application"}]},{"@type":"WebSite","@id":"https:\/\/huongdanjava.com\/#website","url":"https:\/\/huongdanjava.com\/","name":"Huong Dan Java","description":"Java development tutorials","publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/huongdanjava.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d","name":"Khanh Nguyen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","width":1267,"height":1517,"caption":"Khanh Nguyen"},"logo":{"@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg"},"description":"I love Java and everything related to Java.","sameAs":["https:\/\/huongdanjava.com","https:\/\/www.facebook.com\/nhkhanh2406","https:\/\/x.com\/https:\/\/twitter.com\/KhanhNguyenJ"]}]}},"_links":{"self":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/22694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/comments?post=22694"}],"version-history":[{"count":3,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/22694\/revisions"}],"predecessor-version":[{"id":22701,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/22694\/revisions\/22701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media\/1680"}],"wp:attachment":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media?parent=22694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/categories?post=22694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/tags?post=22694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}