如何将spring对象注入cordaApp项目?

如何将spring对象注入cordaApp项目?,corda,Corda,我创建了一个小型spring库,并希望将其集成到CorDapp项目中 我在CorDapp build gradle中添加了所有必需的spring依赖项,但当我运行应用程序时,我的@Autowired类变为null 有人能在这方面提供帮助吗,或者以前有人尝试过这种功能吗 简言之,我想将spring对象注入cordaApp项目,有可能吗 >>>>>> ExampleApi类包含“metoo”REST API,是一个带有@Component注释的类,还具有TestComp作为@Autowired: @

我创建了一个小型spring库,并希望将其集成到CorDapp项目中

我在CorDapp build gradle中添加了所有必需的spring依赖项,但当我运行应用程序时,我的@Autowired类变为null

有人能在这方面提供帮助吗,或者以前有人尝试过这种功能吗

简言之,我想将spring对象注入cordaApp项目,有可能吗

>>>>>>

ExampleApi类包含“metoo”REST API,是一个带有@Component注释的类,还具有
TestComp
作为
@Autowired

@Path("example")
@Component
public class ExampleApi {
    private final CordaRPCOps rpcOps;
    private final CordaX500Name myLegalName;

    public ExampleApi(CordaRPCOps rpcOps) {
        this.rpcOps = rpcOps;
        this.myLegalName = rpcOps.nodeInfo().getLegalIdentities().get(0).getName();
    }

    @Autowired
    TestComp testComp;

    @GET
    @Path("metoo")
    @Produces(MediaType.APPLICATION_JSON)
    public String whoamitest() {
        // here testComp is getting null at runtime
        return testComp.getMyName();
    }
}
===================================================================

TestComp类的示例如下所示:

@Component
public class TestComp {

    public String getMyName()  {
        return "Hello there ...";
    }
}
===================================================================

在com.example包中添加了如下应用程序类

添加了JerseyConfig类,如下所述:

@Configuration
@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("com.example");
        register(ExampleApi.class);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
        // our rest resources will be available in the path /rest/*
        registration.addInitParameter(
        ServletProperties.JAXRS_APPLICATION_CLASS, 
        JerseyConfig.class.getName());
        return registration;
    }
}
============================================================

在build.gradle文件中添加了以下库/依赖项:

compile (group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version: '1.5.9.RELEASE'){
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

compile(group: 'org.springframework.boot',name: 'spring-boot-starter-web' , version: '1.5.9.RELEASE') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.5.9.RELEASE'
classpath group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.5.9.RELEASE', ext: 'pom'
=================================================================

将以下内容添加到outer build.gradle文件中:

compile (group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version: '1.5.9.RELEASE'){
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

compile(group: 'org.springframework.boot',name: 'spring-boot-starter-web' , version: '1.5.9.RELEASE') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.5.9.RELEASE'
classpath group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.5.9.RELEASE', ext: 'pom'

==========================================================

此结构根本无法工作:

  • 一方面,您正在定义
    ExampleApi
    ,并将其列在CorDapp的web插件注册表中。这意味着API将在编译时获取,并转换为默认节点Web服务器(Jetty Web服务器)的一组端点

  • 另一方面,您正在定义一个独立的SpringWebServer


这两个Web服务器作为完全独立的Java进程运行,因此其中一个不能在另一个中插入
自动连线
类。

您可以提供您试图自动连线的示例吗?例如,我们有CordaNetworkService自定义类,其中列出了与网络相关的所有基本/常见操作,如1。getPeers()2。GetPeersWith公证人()3。getNetworkMapService()4。checkIfNotaryParty()5。getPartyCountOnNetwork()及更多。。我们在spring库中将这些常用/通用方法分开,这样它就可以轻松地插入到我们的任何corda项目中,这将有助于我们在更短的时间内开发cordaApp。我认为问题是,corda使用Jax rs[@Path]实现Rest功能,当我们启动corda节点时,它的上下文和我们的spring库上下文是冲突的。任何关于这类功能的想法或例子都会很有帮助。你能链接到复制问题的回购协议吗?我无法复制它。低于错误org.glassfish.jersey.server.spring.SpringComponentProvider initia严重:spring上下文查找失败,跳过spring组件提供程序初始化。