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.
10 - Invoke: Run the mule application and invoke the endpoint to see the result. This will return records from the accounts 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: From a Mule flow it's very simple to access one of the previously 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 this will return Hello from Shyam, which we have configured the first name in beans property.
Comentários