How to use custom Java classes in service builder *impl classes

Liferay Service-Builder mechanism generates a lot of code, it’s very easy to use and it lets you use the Liferay Service API.

If you want to use external classes in your service impl classes you must use a special annotation : @BeanReference (from the package com.liferay.portal.kernel.bean)

Using this annotation you can inject your classes into your service and then use it without having any exceptions on class loading context.

Steps

  1. Declare the bean you want to inject in the spring configuration file of your portlet
  2. <bean id="com.test.service.MyService" class="com.test.service.impl.MyServiceImpl" />
    
  3. Make a reference to this bean in your service impl class
  4. @BeanReference(type = MyService.class)
    private MyService myService;
    

Et voila! Now you can use the service “myService” in your service impl methods 🙂

NB : if you want to understand how the injection is done, have a look at this class : BeanReferenceAnnotationBeanPostProcessor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s