AEM Integration with Spring

11 / Aug / 2015 by Yagyesh

The purpose of this blog is to provide the details on how to integrate Spring in Adobe Experience Manager (AEM) aka CQ5 with the help of OSGI extender. This will open up the world of Spring (DI, AOP, Caching, web services (REST/SOAP), transactions, logging etc.) to AEM projects. This integration will be very vital for the projects, which require data exchange with existing enterprise systems.

We would be using the Spring OSGi Extender bundles. This bundle is responsible for instantiating the Spring application contexts for your application bundles. It serves the same purpose as the ContextLoaderListener does for Spring web applications. Once the extender bundle is installed and started it looks for any existing Spring-powered bundles that are already in the ACTIVE state and creates application contexts on their behalf. In addition, it listens for bundle starting events and automatically creates an application context for any Spring-powered bundle that is subsequently started.






Required Bundles and there version

spring-osgi-extender 2.0.0
spring-osgi-core 2.0.0
spring-osgi-io 2.0.0 1.0.0
org.springframework.core 3.1.1.RELEASE
org.springframework.context 3.1.1.RELEASE
org.springframework.beans 3.1.1.RELEASE
org.springframework.aop 3.1.1.RELEASE
org.springframework.asm 3.1.1.RELEASE
org.springframework.expression 3.1.1.RELEASE 3.1.1.RELEASE

OSGI – spring application context :  org.springframeork.osgi.bundle.extender queries all of the existing bundles in the resolved state to learn if any of them is Spring powered. The extender bundle considers a bundle to be spring powered if it has the Spring-Context manifest header OR if it has XML files in its META-INF/spring folder.

Let us take the second approach and add configuration files into META-INF/spring folder.

Note: META-INF/spring folder should be manually created under src/main/resources folder



applicationContext.xml: This xml file will be having the definitions of the beans used in the same bundle. All the beans defined in this xml will be instantiated once this bundle gets deployed in felix container.
Sample applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:context="" xsi:schemaLocation="">
    <!-- Tell Spring to process annotations -->
    <context:annotation-config />
    <context:component-scan base-package="com.ig.core" />
    <bean id="demoService" class="com.ig.core.impl.DemoService" />
    <bean id="myServlet" class="com.ig.core.MyServlet" />

Osgi-context.xml: Provides a Spring-OSGi Managed Service reference ( slingRepository,workflowmanager,resourceResolverFactory or custom service ) to other implementations.

sample Osgi-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:osgi="" xsi:schemaLocation="">
    <osgi:reference id="slingRepository" interface="" />
    <osgi:reference id="sampleService" interface="com.ig.core.impl.SampleService" />
    <osgi:reference id="resourceResolverFactory" interface="" />

In our next post, we will talk about spring AOP integration using this approach.

To learn about our AEM service, please click here.

Tag -