In this post:
The Spring module enables Mule apps to use the Spring framework. In this article, we will use a database datasource that is created by spring beans. Also, we will invoke one bean’s method from a Mule flow.
Step by step
1 - Create Mule Project: Go to Anypoint Studio and create a new Mule project.
2 - Add spring module: In Anypoint Studio, the Spring module is provided in the default configuration. In the Mule Palette, click on add modules, search for Spring and add this module to your project.
3 - Spring config: Go to spring config, add name as Spring_Config and provide files as beans.xml
4 - Spring Beans: Create a beans.xml file under src/main/resources. Add the below configuration in this file.
5 - Spring JDBC: Add spring JDBC and PostgreSQL dependency and shared library in pom.xml
Add below shared library in the shared libraries tag.
6 - Spring JDBC Beans: Add spring Datasource bean configuration in beans.xml.
7 - Application Properties: Add below application properties for JDBC connection in src/main/resources. You can replace the correct username, password, and database name in the below properties.
8 - Database Config: Go to Mule Palette and drag the database connector. Configure the database configuration in global.xml with the datasource and select the PostgreSQL jar for the JDBC driver.
Test the configuration, this should return a successful connection.
9 - HTTP Listener for accounts: Add an HTTP listener and configure it with the default settings. Add a Select DB configuration and a Select query for the accounts table. Make sure the account table is created in your database and there will be at least one entry for the account table. Add a transformer to give the result in JSON format.