[英]How to inject Spring bean to vertx jersey instance
我正在尝试将vertx jersey框架与Spring一起使用。 我正在尝试注入一个带有Jersey注释的Spring创建的bean,以供vertx jersey框架使用,但注入时遇到了问题。
这是我的主要代码
public class Main {
private final static Logger LOGGER = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application_context.xml");
EComAppController eComAppController = (EComAppController) context.getBean("ecomAppController");
List<Object> instances = new ArrayList<>();
instances.add(eComAppController);
Vertx vertx = Vertx.vertx();
vertx.runOnContext(aVoid -> {
// Set up the jersey configuration
// The minimum config required is a package to inspect for JAX-RS endpoints
vertx.getOrCreateContext().config()
.put("jersey", new JsonObject()
.put("port", 8080)
.put("features", new JsonArray()
.add("org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature"))
/*.put("packages", new JsonArray() //This is working, but then vertx-jersey framework will initialize new object which I dont want.
.add(eComAppController.getClass().getPackage().getName()))*/
.put("instances", instances)//This approach not working
);
// Use a service locator (HK2 or Guice are supported by default) to create the jersey server
ServiceLocator locator = ServiceLocatorUtilities
.bind(new HK2JerseyBinder(), new HK2VertxBinder(vertx));
JerseyServer server = locator.getService(JerseyServer.class);
JerseyOptions options = server.getHandler().getContainer().getOptions();
//options.getInstances().add(eComAppController);
// Start the server which simply returns "Hello World!" to each GET request.
server.start();
//Getting exception on above line
LOGGER.info("Server started successfully");
});
}
}
我正在使用上面的代码得到以下异常
SEVERE: Failed to start the jersey container
java.lang.ClassCastException: com.rt.controller.EComAppController cannot be cast to java.lang.CharSequence
遵循“不要问”的原则。
与其传递EComAppController
(Vert.x尝试序列化),不考虑将这个bean封装在一个verticle中,并使用EventBus与之通信:
class EcomVerticle extends AbstractVerticle {
EComAppController eComAppController;
@Override
public void start() {
ApplicationContext context = new ClassPathXmlApplicationContext("application_context.xml");
this.eComAppController = (EComAppController) context.getBean("ecomAppController");
vertx.eventBus().consumer("ecom-question", (message) -> {
// eComAppController does something with the message
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.