Summery of Important annotations in Liferay DXP
@Component
We will need this annotation to define a component and publish into the Liferay OSGI container
- immediate - ensure the component is started right away and not wait for a reference
- properties - Used to pass in a set of OSGi properties to bind to the component.
- service - Defines the service that the component implements.
- EXAMPLE
/**
* @author manish
*/
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.header-portlet-css=/css/main.css",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=VINTracker",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + VINTrackerPortletKeys.VINTRACKER,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class VINTrackerPortlet extends MVCPortlet {
}
@Reference (All blue are @Reference attribute)
@Reference(
target = "(component.name=override.my.service.reference.service.impl.SomeServiceImpl )",
unbind = "-"
)
private SomeService _defaultService;
- MANDITORY (Default) - The reference must be available and injected before this component will start.
- OPTIONAL - The reference is not required for the component to start and will function w/o a component assignment.
- MULTIPLE - Multiple resources may satisfy the reference and the component will take all of them, but like OPTIONAL the reference is not needed for the component to start.
- AT_LEAST_ONE - Multiple resources may satisfy the reference and the component will take all of them, but at least one is manditory for the component to start.
@Reference(
cardinality = ReferenceCardinality.MULTIPLE,
policy = ReferencePolicy.DYNAMIC,
target = "(com.liferay.fragment.entry.processor.portlet.alias=*)"
)
protected void setPortlet(Portlet portlet, Map<String, Object> properties) {
String aliass = MapUtil.getString(
properties, "com.liferay.fragment.entry.processor.portlet.alias");
String portletName = MapUtil.getString(
properties, "javax.portlet.name");
_portletNames.put(aliass, portletName);
}
- STATIC (Default) - The component will only be started when there is an assigned reference, and will not be notified of alternative services as they become available.
- DYNAMIC - The component will start when there is reference(s) or not, and the component will accept new references as they become available.
@Reference(
policy = ReferencePolicy.STATIC,
policyOption = ReferencePolicyOption.GREEDY,
cardinality = ReferenceCardinality.MANDATORY)
protected void setModbus(BridgeModbus modbus) {
super.setModbus(modbus);
}
- RELUCTANT (Default) - For single reference cardinality, new reference potentials that become available will be ignored. For multiple reference cardinality, new reference potentials will be bound.
- GREEDY - As new reference potentials become available, the component will bind to them.
First is the default, ReferenceCardinality.MANDITORY, ReferencePolicy.STATIC and ReferencePolicyOption.RELUCTANT. This summarizes down to your component must have only one reference service to start and regardless of new services that are started, your component is going to ignore them. These are really good and normal defaults and promote stability for your component.
Another common grouping you'll find in the Liferay source is ReferenceCardinality.OPTIONAL or MULTIPLE, ReferencePolicy.DYNAMIC and ReferencePolicyOption.GREEDY. In this configuration, the component will function with or without reference service(s), but the component allows for changing/adding references on the fly and wants to bind to new references when they are available.
@BeanReference
This is a Liferay annotation used to inject a reference to a Spring bean from the Liferay core.
@ServiceReference
This is a Liferay annotation used to inject a reference from a Spring Extender module bean.
*** If your @Reference isn't getting injected or is null, that will be sign that you should use one of the other reference annotations. Here your choice is easy: if the bean is from the Liferay core, use @BeanReference, but if it is from a Spring Extender module, use the @ServiceReference annotation instead.
@Activate
The @Activate annotation is OSGi's equivalent to Spring's InitializingBean interface. It declares a method that will be invoked after the component has started.
@Deactivate
The @Deactivate annotation is the inverse of the @Activate annotation, it identifies a method that will be invoked when the component is being deactivated.
Reference > https://liferay.dev/blogs/-/blogs/liferay-osgi-annotations-what-they-are-and-when-to-use-them
Comments
Post a Comment