java – Use Spring Beans In Micronaut Applications

java – Use Spring Beans In Micronaut Applications

You can check this there is an example how to integrate Micronaut with Spring

I have created a simple application base on this, code above.
And for example on http://localhost:8080/hello/World it returns: Hello World with Test : World and Test : World. So it looks like Controller uses 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;
    }
}

And

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));
    }
}

UPDATE

A hack that allows to get access to ConfigurableApplicationContext context. In Application create something like singleton or in my example static field that hold your context:

static ConfigurableApplicationContext springContext;

...

ConfigurableApplicationContext springContext = ReactiveConnectorBootable.loadReactiveConnector();

And then in your 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;
}

java – Use Spring Beans In Micronaut Applications

Leave a Reply

Your email address will not be published. Required fields are marked *