I have been looking for something like this.
Having this micronaut app code
@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);
}
}
How can I merge an ApplicationContext of Spring with the one of Micronaut.
I need it since I have a library that use ApplicationContext of Spring to inject some beans.
Then in my controller I need to use it.
@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";
}
}
Where ReactorConnectorManager
is a bean loaded in Spring context
Regards
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";
}