April 10, 2020
Estimated Post Reading Time ~

AEM Integration with Spring

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 their version

BUNDLES VERSIONS
spring-osgi-extender 2.0.0
spring-osgi-core 2.0.0
spring-osgi-io 2.0.0
com.springsource.org.aopalliance 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
org.springframework.context.support 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
Structure:

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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context ;
<!-- 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" />
</beans>

Osgi-context.xml: Provides a Spring-OSGi Managed Service reference ( slang repository, workflow manager,resourceResolverFactory or custom service ) to other implementations.
sample Osgi-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi ;
<osgi:reference id="slingRepository" interface="org.apache.sling.jcr.api.SlingRepository" />
<osgi:reference id="sampleService" interface="com.ig.core.impl.SampleService" />
<osgi:reference id="resourceResolverFactory" interface="org.apache.sling.api.resource.ResourceResolverFactory" />
</beans>

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



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.