Skip to content

Decorators API Reference

All exports from @pandino/decorators.

ts
import { Component, Service, Reference, Activate, Deactivate, Modified } from '@pandino/decorators';

Class Decorators

DecoratorTargetDescription
@Component(options?)ClassDeclares a class as a Declarative Services component
@Service(options?)ClassRegisters the component as a service under specified interfaces
@ImmediateClassMarks the component for immediate activation
@Factory(factoryId)ClassDeclares a factory component
@ConfigurationPolicy(policy)ClassSets the configuration policy
@Scope(scope)ClassSets the service scope
@Property(key, value)ClassAdds a static property to the component

@Component

ts
@Component({
  name: 'com.example.my-service',
  immediate: true,
  configurationPid: 'com.example.config',
})
class MyServiceImpl {}
OptionTypeDefaultDescription
name?stringClass nameUnique component name
immediate?booleanfalseActivate immediately, even without service consumers
enabled?booleantrueWhether the component is enabled at bundle start
configurationPid?stringComponent nameConfiguration Admin PID
configurationPolicy?'optional' | 'require' | 'ignore''optional'How configuration affects activation
factory?string-Factory identifier; makes this a factory component
scope?'singleton' | 'bundle' | 'prototype''singleton'Component instance scope
service?ServiceDescriptor-Inline service registration (alternative to @Service)
properties?Record<string, any>{}Component/service properties
references?ReferenceDescriptor[][]Inline reference declarations
activate?string-Name of the activate method
deactivate?string-Name of the deactivate method
modified?string-Name of the modified method

@Service

ts
@Service({ interfaces: ['MyService', 'Disposable'] })
class MyServiceImpl {}
OptionTypeDefaultDescription
interfaces?string[][ClassName]Interface names to register under
scope?'singleton' | 'bundle' | 'prototype''singleton'Service scope

@Immediate

No options. Shorthand for @Component({ immediate: true }).

ts
@Component({ name: 'my.service' })
@Service({ interfaces: ['MyService'] })
@Immediate
class MyServiceImpl {}

@Factory

ts
@Component({ name: 'my.factory' })
@Factory('my.factory.id')
class MyFactoryComponent {}
ParameterTypeDescription
factoryIdstringUnique identifier for the factory

@ConfigurationPolicy

ts
@Component({ name: 'my.configured.service' })
@ConfigurationPolicy('require')
class MyConfiguredService {}
ParameterTypeDescription
policy'optional' | 'require' | 'ignore''optional' activates with or without config; 'require' needs config to activate; 'ignore' ignores any config

@Scope

ts
@Component({ name: 'my.prototype.service' })
@Service({ interfaces: ['MyService'] })
@Scope('prototype')
class MyPrototypeService {}
ParameterTypeDescription
scope'singleton' | 'bundle' | 'prototype''singleton' = one instance; 'bundle' = one per consuming bundle; 'prototype' = new instance per lookup

@Property

ts
@Component({ name: 'my.service' })
@Property('service.vendor', 'Example Corp')
@Property('service.ranking', 10)
class MyServiceImpl {}
ParameterTypeDescription
keystringProperty key
valueanyProperty value

Field Decorators

@Reference

Injects a service dependency into a field.

ts
@Component({ name: 'my.consumer' })
class MyConsumer {
  @Reference({ interface: 'LogService' })
  private logger?: LogService;

  @Reference({ interface: 'DataService', cardinality: '0..n', policy: 'dynamic' })
  private dataSources?: DataService[];
}
OptionTypeDefaultDescription
name?stringField nameReference name, used to identify bind/unbind methods
interface?string'any'Service interface to bind
cardinality?'1..1' | '0..1' | '1..n' | '0..n''1..1'1..1 = mandatory single; 0..1 = optional single; 1..n = mandatory multiple; 0..n = optional multiple
policy?'static' | 'dynamic''static''static' requires deactivation to rebind; 'dynamic' allows live rebinding
policyOption?'reluctant' | 'greedy''reluctant''reluctant' keeps current binding; 'greedy' rebinds to better matches immediately
target?string-LDAP filter applied to the referenced service properties
bind?string-Name of bind callback method
unbind?string-Name of unbind callback method
updated?string-Name of updated callback method
field?stringField nameTarget field for injection
fieldOption?'replace' | 'update''replace''replace' swaps the field value; 'update' mutates in place
scope?'bundle' | 'prototype' | 'prototype_required''bundle'Required scope for the referenced service

Method Decorators

DecoratorTargetDescription
@ActivateMethodCalled when the component is activated
@DeactivateMethodCalled before the component is deactivated
@ModifiedMethodCalled when bound configuration changes

@Activate

Receives a ComponentContext parameter.

ts
@Component({ name: 'my.service', immediate: true })
class MyServiceImpl {
  @Activate
  activate(context: ComponentContext): void {
    const bundleContext = context.getBundleContext();
    const props = context.getProperties();
  }
}

@Deactivate

ts
@Component({ name: 'my.service', immediate: true })
class MyServiceImpl {
  @Deactivate
  deactivate(): void {
    // cleanup resources
  }
}

@Modified

ts
@Component({ name: 'my.service', configurationPid: 'my.config' })
class MyServiceImpl {
  @Modified
  configChanged(): void {
    // react to configuration updates
  }
}

Interfaces

ExportTypeDescription
ComponentDescriptorInterfaceFull component metadata shape
ReferenceDescriptorInterfaceReference metadata shape
ServiceDescriptorInterfaceService registration metadata
OSGiConstructor<T>TypeConstructor type: new (...args: any[]) => T
COMPONENT_METADATA_KEYConstantReflect metadata key: 'osgi:component'

ComponentDescriptor

PropertyTypeDescription
namestringComponent name
implementationstring | FunctionImplementation class
class?anyDirect class reference
properties?Record<string, any>Component properties
references?ReferenceDescriptor[]Service references
activate?stringActivate method name
deactivate?stringDeactivate method name
modified?stringModified method name
configurationPid?stringConfiguration PID
configurationPolicy?'optional' | 'require' | 'ignore'Configuration policy
factory?stringFactory identifier
immediate?booleanImmediate activation flag
enabled?booleanEnabled at bundle start
scope?'singleton' | 'bundle' | 'prototype'Component scope
service?ServiceDescriptorService descriptor

ReferenceDescriptor

PropertyTypeDescription
namestringReference name
interfacestringTarget service interface
cardinality'1..1' | '0..1' | '1..n' | '0..n'Reference cardinality
policy'static' | 'dynamic'Binding policy
policyOption?'reluctant' | 'greedy'Rebinding strategy
target?stringLDAP filter for the target service
bind?stringBind method name
unbind?stringUnbind method name
updated?stringUpdated method name
field?stringInjection field name
fieldOption?'replace' | 'update'Field update strategy
scope?'bundle' | 'prototype' | 'prototype_required'Required service scope

ServiceDescriptor

PropertyTypeDescription
interfaces?string[]Interfaces to register under
scope?'singleton' | 'bundle' | 'prototype'Service scope

Released under the Eclipse Public License 2.0.