Skip to content

What is @Component annotation

The @Component annotation is fundamental in Liferay’s OSGi (Open Service Gateway Initiative) module system. It declares a Java class as an OSGi component that can provide or consume services. Below is a comprehensive breakdown of its attributes and usage.


  • Purpose: Marks a class as an OSGi Declarative Services (DS) component.
  • Usage: Used to define components that can be managed by the OSGi container.
  • Location: From the org.osgi.service.component.annotations package.

Here’s a table of all possible @Component attributes in Liferay 7.4:

AttributeTypeDefaultDescription
immediatebooleanfalseIf true, the component is activated immediately after registration.
enabledbooleantrueIf false, the component is disabled and won’t start.
serviceClass<?>[]{}Specifies the service interfaces this component provides.
propertyString[]{}OSGi properties for the component (key-value pairs).
propertiesProperty[]{}Alternative to property for more structured properties.
configurationPidString[]{}Associates the component with a Configuration PID.
configurationPolicyConfigurationPolicyConfigurationPolicy.OPTIONALDefines how the component reacts to missing configurations.
scopeServiceScopeServiceScope.SINGLETONDefines the component’s lifecycle (SINGLETON, PROTOTYPE, or BUNDLE).
factorybooleanfalseIf true, the component is a factory component.
nameStringGeneratedExplicitly sets the component name.

@Component(
immediate = true,
service = MyService.class
)
public class MyServiceImpl implements MyService {
// Implementation
}
  • Explanation: Registers MyServiceImpl as an OSGi service under MyService.
@Component(
property = {
"key1=value1",
"key2=value2",
"service.ranking:Integer=100"
},
service = MyService.class
)
public class MyServiceImpl implements MyService {
// Implementation
}
  • Explanation: Adds metadata (key1, key2) and sets service ranking.
@Component(
configurationPid = "com.example.MyConfig",
configurationPolicy = ConfigurationPolicy.REQUIRE
)
public class ConfigurableComponent {
// Requires "com.example.MyConfig" to activate
}
  • Explanation: Only activates if com.example.MyConfig is available.
@Component(
factory = true,
scope = ServiceScope.PROTOTYPE
)
public class PrototypeComponent {
// New instance created per request
}
@Component(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=My",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + MyPortletKeys.MY,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
}, service = Portlet.class)
  • Explanation: Creates a new instance each time the service is requested.

Liferay uses special properties in @Component for framework integration:

PropertyMeaningExample
"osgi.jaxrs.name=..."Registers a JAX-RS resource"osgi.jaxrs.name=my.rest"
"auth.verifier.guest.allowed=true"Allows guest access"auth.verifier.guest.allowed=true"
"service.ranking:Integer=100"Sets service priority"service.ranking:Integer=100"

  1. Use immediate = true for components that must start right away.
  2. Avoid ConfigurationPolicy.REQUIRE unless mandatory (can prevent startup).
  3. Prefer @Reference for service dependencies instead of manual lookups.
  4. Use service.ranking to control service override priority.

IssueSolution
Component not startingCheck immediate=true and dependencies.
Missing serviceVerify @Reference or service= declaration.
Configuration not applyingEnsure configurationPid matches .cfg file.

  • @Component is the backbone of OSGi services in Liferay.
  • It supports flexible registration, configuration, and lifecycle control.
  • Proper use of attributes ensures modularity and maintainability.
  • Explore @Reference for dependency injection.
  • Learn about OSGi Configuration Admin for dynamic settings.
  • Check Liferay’s official docs for advanced patterns.