{"id":24891,"date":"2015-08-11T11:50:14","date_gmt":"2015-08-11T06:20:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=24891"},"modified":"2016-12-19T15:07:36","modified_gmt":"2016-12-19T09:37:36","slug":"aem-integration-with-spring","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aem-integration-with-spring\/","title":{"rendered":"AEM Integration with Spring"},"content":{"rendered":"<p>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.<\/p>\n<p>We would be using the <strong>Spring OSGi Extender<\/strong> bundles. This bundle is responsible for instantiating the Spring application contexts for your application bundles. It serves the same purpose as the <a href=\"http:\/\/docs.spring.io\/autorepo\/docs\/spring-framework\/3.0.x\/api\/org\/springframework\/web\/context\/ContextLoaderListener.html\">ContextLoaderListener<\/a>\u00a0does 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\u00a0ACTIVE\u00a0state 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.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-24892\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/08\/sp1.png\" alt=\"sp1\" width=\"595\" height=\"344\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-24893\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/08\/test.png\" alt=\"test\" width=\"552\" height=\"541\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Required Bundles and there version<\/p>\n<table width=\"421\">\n<tbody>\n<tr>\n<td width=\"259\"><strong>BUNDLES<\/strong><\/td>\n<td width=\"162\"><strong>VERSIONS<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"259\">spring-osgi-extender<\/td>\n<td width=\"162\">2.0.0<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">spring-osgi-core<\/td>\n<td width=\"162\">2.0.0<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">spring-osgi-io<\/td>\n<td width=\"162\">2.0.0<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">com.springsource.org.aopalliance<\/td>\n<td width=\"162\">1.0.0<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.core<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.context<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.beans<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.aop<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.asm<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.expression<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<tr>\n<td width=\"259\">org.springframework.context.support<\/td>\n<td width=\"162\">3.1.1.RELEASE<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>OSGI \u2013 spring application context :\u00a0<\/strong> 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\u00a0if it has the\u00a0Spring-Context\u00a0manifest header OR if it has XML files in its\u00a0META-INF\/spring\u00a0folder.<\/p>\n<p>Let us take the second approach and add configuration files into META-INF\/spring folder.<\/p>\n<p>Note: <strong>META-INF\/spring<\/strong> folder should be manually created under src\/main\/resources folder<\/p>\n<p>Structure:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-25406\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/08\/structure.png\" alt=\"structure\" width=\"389\" height=\"406\" \/><\/p>\n<p><strong>applicationContext.xml<\/strong>: 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.<br \/>\n<strong>Sample applicationContext.xml<\/strong><\/p>\n<p>[xml]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd   http:\/\/www.springframework.org\/schema\/context ;<br \/>\n    &lt;!&#8211; Tell Spring to process annotations &#8211;&gt;<br \/>\n    &lt;context:annotation-config \/&gt;<br \/>\n    &lt;context:component-scan base-package=&quot;com.ig.core&quot; \/&gt;<br \/>\n    &lt;bean id=&quot;demoService&quot; class=&quot;com.ig.core.impl.DemoService&quot; \/&gt;<br \/>\n    &lt;bean id=&quot;myServlet&quot; class=&quot;com.ig.core.MyServlet&quot; \/&gt;<br \/>\n&lt;\/beans&gt;<br \/>\n[\/xml]<\/p>\n<p><strong>Osgi-context.xml:\u00a0<\/strong>Provides a Spring-OSGi Managed Service reference ( slingRepository,workflowmanager,resourceResolverFactory or custom service ) to other implementations.<\/p>\n<p><strong>sample Osgi-context.xml:<\/strong><\/p>\n<p>[xml]<br \/>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:osgi=&quot;http:\/\/www.springframework.org\/schema\/osgi&quot; xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd http:\/\/www.springframework.org\/schema\/osgi ;<br \/>\n    &lt;osgi:reference id=&quot;slingRepository&quot; interface=&quot;org.apache.sling.jcr.api.SlingRepository&quot; \/&gt;<br \/>\n    &lt;osgi:reference id=&quot;sampleService&quot; interface=&quot;com.ig.core.impl.SampleService&quot; \/&gt;<br \/>\n    &lt;osgi:reference id=&quot;resourceResolverFactory&quot; interface=&quot;org.apache.sling.api.resource.ResourceResolverFactory&quot; \/&gt;<br \/>\n&lt;\/beans&gt;<\/p>\n<p>[\/xml]<\/p>\n<p>In our next post, we will talk about spring AOP integration using this approach.<\/p>\n<p>To learn about our AEM service, please <a href=\"http:\/\/www.tothenew.com\/wcm\/cq-aem-development-consulting\">click here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":82,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":78},"categories":[1],"tags":[4847,4850,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24891"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=24891"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/24891\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=24891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=24891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=24891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}