Introduction

Often when developing more advanced Java adapters (and JAX-RS services in general)
The need for writing more modular, testable and decoupled code is raising.

Leveraging dependency injection design pattern can be very helpful in those cases.
There are many dependency injection frameworks out there, the reason for choosing Spring
was that it is well known and mature DI framework and also it provides much more
than just DI.

What is dependency injection

Dependency injection is a software design pattern in which an object always receives its dependencies from the outside (constructor/setters) instead of creating them using the new keyword or using some other way.

This way, it make sense to let classes to depend only on the interfaces of their dependencies so that the actual implementation of a dependency will be changeable without changing the code of the dependant class.

Dependency injection in adapters

In this blog post I will explain how to integrate Spring into a MobileFirst Java adapter by using the Spring framework integration for MobileFirst adapters module, so that you will be able to write your adapter’s code using dependency injection. Your code will become much more modular, clean and testable this way.

As you can see, you can use JAXRSResourcesRegistryImpl to specify list of JAX-RS resources (and providers) to be used in your adapter.
In this case we took the resource we already have in the adapter (com.sample.MySpringXmlAdapterResource) and defined it as a spring bean in the list of resources.

At this point the adapter should be ready to run and it should behave exactly the same as the new Java adapter we created in the first step.
You can go on and build & deploy the adapter to see that it is working normally.

The benefits of using Spring in our adapter

Now, let’s see what are the benefits of this spring integration.

Loose coupling is now possible thanks to dependency injection

Perhaps you would like to architect the adapter in such a way that you separate the JAX-RS resources from the business logic.
The resource gets the request and handles the path/content type mapping, but the service that actually does the work is another object.

This architecture gives you the flexibility to have a loose dependency between the JAX-RS resource and that service.
The JAX-RS resource will know the service only by it’s interface, the actual implementation will be resolved at runtime. And here is where spring comes into the picture.

Let’s define our simple “hello” service as a Java interface in our adapter:

Important to notice that the resource class (MySpringXmlAdapterResource.java) knows the service only by the interface. Which implementation to use was decided
in the applicationContext.xml file. Now we can seamlessly replace implementations of hello service without affecting the resources classes code.

Integration with MobileFirst server side configuration API

Another benefit of using this Spring integration module is that it seamlessly integrates MobileFirst adapter configuration feature with Spring’s properties mechanism.

In the following example I will show how to make the hello service configurable by externalising the message it returns.

The result should the following, because it is using the default value defined in the adapter.xml:

hello!!!

To change the value to something else, open the MobileFirst Oprations Console (http://localhost:9080/mfpconsole) and click on mySpringXmlAdapter in the sidebar navigation. Then, in the Configurations tab change the value of “Hello message” field to something, “Hello World” else and click “Save”.