本文介绍了如何在 Neo4j 非托管扩展中通过 @Context 提供您的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Neo4j 非托管扩展.我希望将某些服务创建为单例,并通过我的资源中的 @Context 提供.

I have Neo4j unmanaged extension. I want some services to be created as singletons and be available via @Context in my resources.

像这样:

@Path("/example")
public class ExampleResource {

    public ExampleResource(@Context CostlyService costlyService) { // <<---
        // use it here
    }
}

如何实现?

推荐答案

Neo4j 有 PluginLifecycle 接口让我们有可能挂钩 Neo4j 服务器生命周期并提供我们自己的注入服务 博文.

Neo4j has PluginLifecycle interface that give us possibility to hook into Neo4j server lifecycle and provide our own services for injection blog post.

所以,我们有服务.我们以这个为例:

So, we have service. Let's take this one as example:

public interface CostlyService {
}

public class CostlyServiceImpl implements CostlyService {

    public CostlyService() {
        // a LOT of work done here
    }

    //...
}

现在我们需要制作我们自己的PluginLifecycle实现:

Now we need to make our own PluginLifecycle implementation:

public class ExamplePluginLifecycle implements PluginLifecycle {

    @Override
    public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService,
                                           Configuration config) {
        final List<Injectable<?>> injectables = new ArrayList<>();
        return injectables;
    }

    @Override
    public void stop() {
    }
}

如您所见,可注入列表目前为空.我们很快就会在那里添加我们的服务.

As you see, injectable list is empty for now. We will add our service there soon.

重要提示:您必须注册您的 PluginLifecycle 实现,以便它可以通过 SPI 使用:

Important: you must register your PluginLifecycle implementation, so it will be available via SPI:

// file: META-INF/services/org.neo4j.server.plugins.PluginLifecycle
my.company.extension.ExamplePluginLifecycle

这将使您的 PluginLifecycle 可以被 Neo4j 服务器发现.

This will make your PluginLifecycle discoverable by Neo4j server.

现在我们需要创建实际的可注入对象.让我们为 Injectable 接口编写实现:

Now we need to create actual injectable. Let's write implementation for Injectable interface:

public final class TypedInjectable<T> implements Injectable<T> {

    private final T value;
    private final Class<T> type;

    private TypedInjectable(final T value, final Class<T> type) {
        this.value = value;
        this.type = type;
    }

    public static <T> TypedInjectable<T> injectable(final T value, final Class<T> type) {
        return new TypedInjectable<>(value, type);
    }

    @Override
    public T getValue() {
        return value;
    }

    @Override
    public Class<T> getType() {
        return type;
    }
}

这将作为我们服务的简单容器.用法:

This will serve as simple container for our service. Usage:

import static my.company.extension.TypedInjectable.injectable;

injectable(new CostlyServiceImpl(), CostlyService.class);

现在我们可以将我们的可注入对象添加到 PluginLifecycle 中.

Now we can add our injectable into PluginLifecycle.

@Override
public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService,
                                       Configuration config) {
    final List<Injectable<?>> injectables = new ArrayList<>();
    injectables.add(injectable(new CostlyServiceImpl, CostlyService.class)); // <<---
    return injectables;
}

此更改后,我们的 CostlyService 将可通过 @Context 用于我们的资源:

After this change our CostlyService will be available for our resources via @Context:

@Path("/example")
public class ExampleResource {

    public ExampleResource(@Context CostlyService costlyService) {
        // use it here
    }

    // ...
}

提示:将您的 PluginLifecycle 与您的资源放在同一个包或子包中.

Tip: keep your PluginLifecycle's in same package or in subpackage with your resources.

这篇关于如何在 Neo4j 非托管扩展中通过 @Context 提供您的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 05:18