Another Integration Blog

We are excited to bring you a publication dedicated to sharing insights, stories, and how-tos by…

Follow publication

Spring Module Integration In Mule Application

--

The Spring module enables Mule apps to use the Spring framework. In this article, we will use a database datasource which 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 7, the Spring module is provided in the default configuration. In 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 below configuration in this file.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd"
>

</beans>

5. Spring JDBC: Add spring JDBC and PostgreSQL dependency and shared library in pom.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.4</version>
</dependency>

Add the below-shared library in the shared libraries tag.

<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</sharedLibrary>

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

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>

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.

spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=username
spring.datasource.password=password

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. Make sure the account table is created in your database. 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 empty records from the account table.

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

User.java

package com.shyam.model;
public class User {

private String firstName;
private String lastName;
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

UserService.java

package com.shyam.service;
public interface UserService {

public String sayHello();
}

UserServiceImpl.java

package com.shyam.service;
import com.shyam.model.User;
public class UserServiceImpl implements UserService {

private User user;
@Override
public String sayHello() {
return "Hello from " + user.getFirstName();
}

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}import com.shyam.model.User;

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

<bean id="user" class="com.shyam.model.User">
<property name="firstName" value="Shyam" />
</bean>
<bean id="userService" class="com.shyam.service.UserServiceImpl">
<property name="user" ref="user" />
</bean>

13. Mule Flow: Mule flow is very simple to access one of the above-created bean. 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 bean's property.

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

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

Happy Learning!

References:

--

--

Another Integration Blog
Another Integration Blog

Published in Another Integration Blog

We are excited to bring you a publication dedicated to sharing insights, stories, and how-tos by the best and brightest in the integration/automation industry. Whether you are an avid MuleSoft fan or just starting to explore the ecosystem, this publication has something for you!

Shyam Raj Prasad
Shyam Raj Prasad

Written by Shyam Raj Prasad

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

Responses (1)

Write a response