在Micronaut应用程序中使用春豆

时间:2019-10-20 19:58:24

标签: java spring micronaut

我一直在寻找这样的东西。

具有此micronaut应用程序代码

@Configuration
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
        org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class,
        org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.class,
        org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class})
@ComponentScan(basePackageClasses = HelloController.class)
public class Application {

    public static void main(String[] args) throws Exception {

        System.setProperty("akka.config", "/Users/nb38tv/workspace/hello-micronaut/src/main/resources/config/");
        System.setProperty("akka.config.file", "application.conf");
        ConfigurableApplicationContext springContext = ReactiveConnectorBootable.loadReactiveConnector();

        ConfigurableApplicationContext context = new MicronautApplicationContext();
        context.setParent(springContext);
        context.start();

        Micronaut.run(Application.class);

    }

}

如何将Spring的ApplicationContext与Micronaut的其中一个合并。

我需要它,因为我有一个使用Spring的ApplicationContext注入一些bean的库。

然后在我的控制器中,我需要使用它。

@Controller("/hello")
public class HelloController {

    @Inject
    private ReactorConnectorManager<RequestInfoDTO, String> reactorConnectorRestManagerExecutor;

    @Get(produces = MediaType.TEXT_PLAIN)
    public String index() {
        if(reactorConnectorRestManagerExecutor == null){
            return "boooo";
        }
        return "Hello World";
    }
}

ReactorConnectorManager是在Spring context中加载的bean

致谢

1 个答案:

答案 0 :(得分:1)

您可以检查this,这里有一个如何集成Micronaut with Spring的示例

我已经在上面的代码this的基础上创建了一个简单的应用程序。 例如,在http://localhost:8080/hello/World上它返回:Hello World with Test : World and Test : World。因此,看来Controller使用Bean

package greeting.example;

import io.micronaut.runtime.Micronaut;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.function.Function;

@SpringBootApplication
public class Application {

    public static void main(String... args) {
        Micronaut.run(Application.class);
    }

    @Bean
    Function<String, String> stringService(){
        return (value) -> "Test : "+ value;
    }
}

package greeting.example;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;

import javax.inject.Inject;
import java.util.function.Function;

@Controller("/")
public class HelloController {

    @Inject
    Function<String, String> stringService;

    @Autowired
    private ApplicationContext applicationContext;

    @Get(uri = "/hello/{name}", produces = MediaType.TEXT_PLAIN)
    public Single<String> hello(String name) {
        Function<String, String> service =  applicationContext.getBean(Function.class);
        return Single.just("Hello " + name + " with " + stringService.apply(name)
                + " and " + service.apply(name));
    }
}

更新

一种允许访问ConfigurableApplicationContext context的黑客。在Application中创建类似于singleton的示例,或在我的示例静态字段中创建保存您的context的示例:

static ConfigurableApplicationContext springContext;

...

ConfigurableApplicationContext springContext = ReactiveConnectorBootable.loadReactiveConnector();

然后在您的HelloController中:

@Get(produces = MediaType.TEXT_PLAIN)
public String index() {
    ReactorConnectorManager<RequestInfoDTO, String> reactorConnectorRestManagerExecutor =
            Application.springContext.getBean(ReactorConnectorManager<RequestInfoDTO, String>.class);
    if(reactorConnectorRestManagerExecutor  == null){
        return "boooo";
    }
    return "Hello World";
}