Spring Module Integration In a Mule Application

Shyam Raj Prasad
Another Integration Blog
3 min readAug 4, 2022

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 mule flow.

  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 the name as Spring_Config, and provide files as beans.xml

4. Spring Beans: Create beans.xml 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 the below shared library in the shared libraries tag.

6. Spring JDBC Beans: Add Spring Datasource bean configuration in beans.xml

7. Application Properties: Add the 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 database modules. Configure database configuration in global.xml with datasource and select PostgreSQL jar for JDBC driver.

Test the configuration, this should return a successful connection.

9. HTTP Listener for accounts: Add HTTP listener and configure it with default settings. Add select DB configuration and add a select query for the accounts table. Ensure 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.

10. Invoke: Run the mule application Invoke the endpoint and see the result. This will return records from the account table.

11. Create Spring Beans: Create User POJO class, UserService interface, and UserServiceImpl service implementation class.

User.java

UserService.java

UserServiceImpl.java

12. Spring Bean Configuration: Add below Spring Bean configuration for User and userServiceImpl class in beans.xml.

13. Mule Flow: Mule flow is very simple to access one of the above-created beans. It just uses an Invoke component to call the Spring bean’s function.

14. JAVA Invoke: In the Invoke component in the Mule application, we are simply calling the sayHello() method of the UserServiceImpl. Here is the screenshot of the configuration of the Invoke component:

15. Invoke: Invoke the endpoint and see if this will return Hello from Shyam, for which we have configured the first name in the beans property.

In this article, we have learned how to create a spring bean configuration and java invokes a function of the bean’s method.

Github Repo: https://github.com/shyamrajprasad/mule-spring-integration

References:

https://docs.mulesoft.com/spring-module/1.3/

https://spring.io/

--

--

Shyam Raj Prasad
Another Integration Blog

Engineering Leader at Tricon Infotech Private Limited | Mulesoft Certified Developer and Architect