Based on the Java SPI mechanism, it provides annotation, dependency injection, and configuration functionalities.
Define an extension interface and use the @Extensible annotation to declare the extension.
The following defines an extension interface for AgentService:
@Extensible("AgentService")
public interface AgentService {
CompletableFuture<Void> start();
CompletableFuture<Void> stop();
}Implement the extension interface and use the @Extension annotation to declare the extension implementation.
The following implements a file synchronization service LiveSpaceFileSyncer:
@Injectable
@Extension("LiveSpaceFileSyncer")
@ConditionalOnProperty(name = SyncConfig.SYNC_LIVE_SPACE_TYPE, value = "file")
@ConditionalOnProperty(name = GovernanceConfig.CONFIG_LIVE_ENABLED, matchIfMissing = true)
public class LiveSpaceFileSyncer extends AbstractFileSyncer<List<LiveSpace>> {
private static final String CONFIG_LIVE_SPACE = "livespaces.json";
@Inject(PolicySupervisor.COMPONENT_POLICY_SUPERVISOR)
private PolicySupervisor policySupervisor;
@Config(SyncConfig.SYNC_LIVE_SPACE)
private SyncConfig syncConfig = new SyncConfig();
}Annotations on this class:
- The
@Extensionannotation declares the extension implementation and provides a name. - The
@ConditionalOnPropertyannotation declares the conditions for enabling, allowing multiple conditions to be combined. - The
@Injectableannotation declares that the type requires automatic injection. - The
@Injectannotation declares that the field requires automatic injection. - The
@Configannotation declares that the field requires automatic configuration.
- Configure the full path name of the extension in the SPI file
META-INF/services/com.jd.live.agent.core.service.AgentService:com.jd.live.agent.implement.service.policy.file.LiveSpaceFileSyncer - Configure the live space synchronization type as
file.
This annotation is declared on the extension interface with the following configuration parameters:
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | Extension interface name |
This annotation is declared on the extension implementation with the following configuration parameters:
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | Extension implementation name | |
| order | int | Short.MAX_VALUE | Priority, the smaller the value, the higher the priority |
| singleton | boolean | true | Whether it is a singleton |
Conditions can be configured on the extension implementation.
This annotation declares the configuration item switch for enabling. Multiple @ConditionalOnProperty annotations can be configured, with an AND relationship between each.
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | Configuration value | |
| name | String[] | Configuration name array | |
| matchIfMissing | boolean | true | Whether to match if not configured |
| relation | ConditionalRelation | OR | Relation OR or AND |
| caseSensitive | boolean | false | Case sensitivity |
| comparison | enum | EQUAL | comparison EQUAL or NOT_EQAUL |
This annotation declares multiple configuration item switches, consisting of multiple @ConditionalOnProperty annotations, with a relationship between each.
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | ConditionalOnProperty[] | Configuration item array | |
| relation | ConditionalRelation | AND | Relation OR or AND |
This annotation declares the type condition for enabling. If the type exists, it matches. Multiple @ConditionalOnClass annotations can be configured, with an AND relationship between each.
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | Full path class name |
This annotation declares the type condition for enabling. If the type does not exist, it matches. Multiple @ConditionalOnMissingClass annotations can be configured, with an AND relationship between each.
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | Full path class name |
This annotation is declared on the extension implementation with the following configuration parameters:
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | boolean | true | Whether to enable injection |
This annotation is declared on fields of the extension implementation with the following configuration parameters:
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| value | String | "" | Component name |
| nullable | boolean | false | Whether the value can be null |
Declare on a field to inject the system component with the specified name:
@Inject(Application.COMPONENT_APPLICATION)
private Application application;Common system components include:
| Name | Type | Description |
|---|---|---|
| AgentConfig.COMPONENT_AGENT_CONFIG | AgentConfig | Agent configuration |
| EnhanceConfig.COMPONENT_ENHANCE_CONFIG | EnhanceConfig | Enhancement configuration |
| AgentPath.COMPONENT_AGENT_PATH | AgentPath | Agent path |
| Application.COMPONENT_APPLICATION | Application | Application instance |
| Timer.COMPONENT_TIMER | Timer | Timer wheel |
| ClassLoaderConfig.COMPONENT_CLASSLOADER_CONFIG | ClassLoaderConfig | Class loader configuration |
| AgentLifecycle.COMPONENT_AGENT_LIFECYCLE | AgentLifecycle | Agent lifecycle management |
| Registry.COMPONENT_REGISTRY | Registry | Registry |
| PolicySupervisor.COMPONENT_POLICY_SUPERVISOR | PolicySupervisor | Policy supervisor |
| InvocationContext.COMPONENT_INVOCATION_CONTEXT | InvocationContext | Flow control intercept context |
Declare on an extension interface field to inject the highest priority implementation of that extension:
@Inject
private LoadBalancer loadBalancer;Declare on a map field of the extension interface to inject all extension implementations:
@Inject
private Map<String, LoadBalancer> loadBalancers;Declare on an event publisher field to inject the event publisher with the specified name:
@Inject(Publisher.POLICY_SUBSCRIBER)
private Publisher<PolicySubscriber> policyPublisher;