Using Spring Beans in Mule
Mule has been paired with Spring since its creation in 2004. Both use a streamlined POJO model and XML schema to allow powerful integration. In this tutorial, I am going to show how easy it is to use Spring beans in Mule applications by creating three separate projects.
Simple XML Bean Configuration
In this project, I will try to show how to configure the Spring beans in an XML configuration file.

The Spring Beans
In this project, I have two beans: User.java and UserSRVImpl.java. In UserSRVImpl.java, there is a dependency on the User.java class.
User.java:
package com.anupam.bean;
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;
}
}
UserSRVImpl.java:
package com.anupam.service;
import com.anupam.bean.User;
public class UserSRVImpl implements UserSRV {
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;
}
}
The Mule Flow
The Mule flow is very simple. It just uses an Invoke component to call the Spring bean’s function. For more additional info Mule Training

The most important part is the configuration of the Spring beans.
<spring:beans>
<spring:bean id="userBean" class="com.anupam.bean.User">
<spring:property name="firstName" value="Mule" />
</spring:bean>
<spring:bean id="userService" class="com.anupam.service.UserSRVImpl">
<spring:property name="user" ref="userBean" />
</spring:bean>
</spring:beans>
In the code snippet above, I am setting the firstName property of the User bean. You can see that the UserSRVImpl is being injected with a dependency of the User bean.
Migrating the Spring Module
Mule 3 allowed to define Spring beans as part of the Mule application using the Spring DSL directly. This approach requires Mule applications to be exposed to the Spring versions and components that the runtime uses internally. This causes two problems:
- Users were limited to the Spring version and modules available in the Mule runtime.
- Any changes or upgrades in the runtime can potentially affect applications defining. Learn more practical skills from Mulesoft Online Training
<mule>
<spring:beans>
<spring:bean name="xaConnectionFactory" class="org.apache.activemq.ActiveMQXAConnectionFactory">
<spring:property name="brokerURL" value="vm://localhost?broker.persistent=false&broker.useJmx=false" />
</spring:bean>
</spring:beans>
<flow>
...
</flow>
</mule>
Mule 4 example:
<mule>
<spring:config name="springConfig" files="config/connection-factory-beans.xml"/>
<flow>
...
</flow>
</mule>
The Spring module doesn’t ship with any particular Spring dependency. You can provide whatever ones you need by adding the following to your application’s pom.xml:
<properties>
<springVersion>4.1.9.RELEASE</springVersion>
<springSecurityVersion>4.0.4.RELEASE</springSecurityVersion>
</properties>
<build>
<plugins>
<!-- Only used to declare the shared libraries-->
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<sharedLibraries>
<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</sharedLibrary>o <sharedLibrary>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</sharedLibrary>
<sharedLibrary>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</sharedLibrary>
</sharedLibraries>
</configuration>
</plugin>
</plugins>
</build>
To use the Spring module, add it to your application using the Studio palette or add the following dependency in your pom.xml file:
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-spring-module</artifactId>
<version>1.1.0</version> <!-- or newer -->
<classifier>mule-plugin</classifier>
</dependency>
To get in-depth knowledge, enroll for live free demo on Mulesoft Training