As many of you know that Spring 3.1 has one interesting and useful feature called bean definition profiles. Today I am going to demonstrate spring profile in liferay Spring portlet
Note: You can download the full portlet with source code at the end of this blog
Let us quickly understand what is Spring profile first?
Spring profile is the registration or definition of different beans for different environment. Each defined bean is part of profile and you can activate that profile when required. Bean definitions that are part of a profile and has not been activated are simply skipped.
To understand spring profile with Liferay spring portlet, let me take one example.
One of our most frequent request is to provide a mechanism to access properties from different environment like development, production, QA environment etc. For example domain name, every environment has different domain names and to access that we need to define each and every property related to domain names into separate property file. and main thing is at the time of deployment we need to generate different war files for all environment because they all does not have same property accordingly.
Now let us resolve this issue with spring profiling. I am considering you have created liferay spring portlet skeleton and following are the steps for adding profiling in that.
- First of all create separate property files for all environment in /resources folder. For example "portlet_dev.properties" for development, "portlet_prod.properties" for production etc.
- Create "portlet.properties" also for common properties between all environments
- Now load these all property files into spring portlet XML configuration with below code
<!-- For local environment --><beans profile="local"><bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><!-- environment-specific settings --><value>classpath:portlet_local.properties</value></list></property></bean></beans><!-- For Development environment --><beans profile="dev"><bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><!-- environment-specific settings --><value>classpath:portlet_dev.properties</value></list></property></bean></beans>
- As of now consider we have only one environment related property called "domain.name=http://localhost:8080/" for local and "http://<dev-url>:<port-number>/" for development and I wanted to access this into my portlet
- So far we have loaded all property files into portlet spring configuration XML. but how to access properties from those files? With the help of following snippet you can achieve this
<bean id="constant"
class="com.techblog.demo.springprofile.controller.ProfileConstant">
<property name="domainName" value="${domain.name}"/>
</bean>
- Make sure you can not access value from these property files with normal way, for that you have to use '$' sign as per above snippet
- At last we need to mention environment name on server start-up arguments, for that enter below line in server argument list for local environment
-Dspring.profiles.active=local
OR by adding below line into Tomcat’s catalina.properties
spring.profiles.active=local
For other environment you just need to replace "local" String with updated name, but make sure this name should match with bean profile name from spring configuration XML file
So, that’s all about it: you have created your Liferay Spring portlet with Spring profiling functionality, now at the time of deployment you don't need to create separate war file for every different environment as well as you don't require to put all common properties into every property files.
I hope this blog can help when you wanted to use profiling in liferay spring portlet. If you have any trouble then contact me at vijay.gohil2050@gmail.com
Download portlet source code from below link :
https://drive.google.com/file/d/0B8BMXH1AI42wV2Y3d2tzY21MakU/edit?usp=sharing
-Vijay Gohil
Excellent start and nice road map with profiling.
ReplyDeleteThank you for sharing this.
-
Sagar Vyas