When you see the blog title, The question arises, is what's new in this post?
Everyone knows how to create Spring MVC Portlets!
But do we know how to create a portlet which is OSGI enabled and can run parallel with other osgi bundles inside Liferay.
If you have been through these links and many more -
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/spring-mvc
https://github.com/vernaillen/liferay7-springmvc-portlet
You must have noticed it's the same old way which includes all required jars inside the application's lib because we do not have support of global library now.
Liferay 7 is OSGI way now and by OSGI convention you do not include library into each other, but you share them from same place :)
Basic diagram of OSGI, copied from -
To make it work for spring, we need to do two things
- We will be using Apache Service Mix Spring Bundles(OSGI bundles for spring)
- Support of few maven plugins, major one is Maven Bundle Plugin to add require entries to the manifest file and prepare it like a bundle but in a war type.
You can find the source code here - https://github.com/bardiavipin/osgi-spring
Meanwhile, you are checking out source code, I will explain minimal changes in your spring portlets to make them bundles.
Create a basic Spring portlet from your IDE or from command prompt or manually with Maven.
Copy below dependencies to deploy folder
Apache ServiceMix :: Bundles :: spring-beans (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-web (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-core (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-aop (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-expression (4.3.1.RELEASE_1) com.github.ben-manes.caffeine (2.3.2) Apache ServiceMix :: Bundles :: spring-context-support (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-context (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: spring-webmvc (4.3.1.RELEASE_1) Apache ServiceMix :: Bundles :: commons-configuration (1.9.0.2) Apache ServiceMix :: Bundles :: spring-webmvc-portlet (4.3.1.RELEASE_1)
Note:Caffeine is a required library for one for the spring modules.
You can check the status of bundles from Gogo Shell
Now we will be going through major changes inside Portlet files-
Context file for sample portlet is - spring portlet xml
<!--Not Working--><!--<context:component-scan base-package="com.osgi.spring" />-->
<bean class="com.osgi.spring.PortletViewController"/> <!-- Handler mappings for annotation based controllers --><bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
As you can see in the context file, component scan is commented as it is not working for this example. I used the workaround to create simple bean and pass the controller class. Spring players can tell me what is the issue here :)
BND Dependency Fix - BNDDependencyFix
@SuppressWarnings("unused") public class BNDDependencyFix { static { System.out.println(ModelAndViewResolver.class);
When you deploy the portlet without this class, you get errors like this -
java.lang.ClassNotFoundException: org.springframework.context.config.ContextNamespaceHandler cannot be found.
In this class we are initializing or loading the necessary classes from static method.
If you find more errors with same type, you can add your class name here.
Major Maven Plugins - pom.xml
- War plugin to exclude all jars from final war
- Bundle plugin make necessary changes in Manifest file to create it a bundle
- Shade plugin to make single files from spring.schemas and spring.handlers and transform them to a single file independently
- Truezip plugin to move spring.handlers and spring.schemas to classes/META-INF
When you are done with building and deploying this portlet, you can see your portlet is interacting to another Liferay bundle - portal-kernel to fetch release information. Each of the library is deployed as OSGI only and shared between applications.
You can include jar as well inside apps lib with little change in configuration, but until and unless it is necessary don't break the architecture :)
You can use Maven Bundle plugin for any of your independent project and convert it to a osgi bundle.
You are just done, Try & Enjoy the function.............:)