gpt4 book ai didi

java - 在 Micronaut 应用程序中使用 Spring Bean

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:43:23 25 4
gpt4 key购买 nike

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

拥有这个 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 来注入(inject)一些 bean 的库。

然后在我的 Controller 中我需要使用它。

@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

问候

最佳答案

可以查看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 上下文 的 hack。在 Application 中创建类似于单例的东西,或者在我的示例静态字段中创建您的 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";
}

关于java - 在 Micronaut 应用程序中使用 Spring Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com