[英]Grizzly, Jersey and Spring auto-wiring issue
I am using Grizzly as a web server to create standalone REST service with Jersey. 我将Grizzly用作Web服务器,以使用Jersey创建独立的REST服务。 I also want to use Spring IoC in the application but I ran into problems with auto-wiring beans. 我也想在应用程序中使用Spring IoC,但是我遇到了自动装配bean的问题。
To create the server I run this in my main() method: 要创建服务器,请在main()方法中运行此服务器:
HttpServer server = null;
try {
ResourceConfig rc = new PackagesResourceConfig("");
ConfigurableApplicationContext cac = new ClassPathXmlApplicationContext("classpath*:/spring-context.xml");
IoCComponentProviderFactory factory = new SpringComponentProviderFactory(rc,
server = GrizzlyServerFactory.createHttpServer("http://localhost:5555", rc, factory);
System.in.read();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (server != null) {
server.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
My spring-context.xml contains just below: 我的spring-context.xml包含以下内容:
<context:annotation-config />
<context:component-scan base-package="com.test"/>
I want to auto-wire BO object in my Jersey resource class: 我想在我的Jersey资源类中自动绑定BO对象:
@Path("/test")
@Service
public class Test {
@Autowired
private testBO testBo;
....
@GET
@Path("/info")
public String getInfo() {
return testBo.get();
}
}
Now, whenever I call a method in the Test class that references testBo, I am getting NullPointerException , which I think means the bean was not auto-wired. 现在,每当我在Test类中调用引用testBo的方法时,我都会得到NullPointerException ,我认为这意味着该bean没有自动接线。
testBO class has @Component annotation. testBO类具有@Component批注。 Also all relevant beans are part of "com.test" package or its subpackages. 同样,所有相关的bean都是“ com.test”包或其子包的一部分。
Anyone has any idea what am I doing wrong? 有人知道我在做什么错吗?
As requested stack trace for NullPointerException below: 根据以下NullPointerException的请求堆栈跟踪:
java.lang.NullPointerException
at com.test.Test.getInfo(Test.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer._service(GrizzlyContainer.java:215)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer.service(GrizzlyContainer.java:185)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:162)
at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:195)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:162)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:160)
at org.glassfish.grizzly.filterchain.ExecutorResolver$3.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:444)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:364)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:290)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:133)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:76)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:63)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:823)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:116)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$000(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$1.run(WorkerThreadIOStrategy.java:98)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:508)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:488)
at java.lang.Thread.run(Thread.java:662)
I am also seeing that in the console output: 我还在控制台输出中看到了这一点:
03-Aug-2013 17:46:33 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@131f71a: startup date [Sat Aug 03 17:46:33 BST 2013]; root of context hierarchy
03-Aug-2013 17:46:33 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1971afc: defining beans []; root of factory hierarchy
If you have Java 8 - might be worth taking a look at Microserver . 如果您有Java 8,也许值得一看Microserver 。
It provides the infrastructure to integrate Grizzly, Jersey 2 and Spring 4.0. 它提供了用于集成Grizzly,Jersey 2和Spring 4.0的基础结构。 If your main method is in package com.test, then all you need is :- 如果您的主要方法是在com.test软件包中,那么您需要做的是:
public static void main(String[] args){
new MicroserverApp(()->"context").run();
}
Otherwise, you should also annotate your main class with 否则,您还应该使用
@Microserver(basePackages={"com.test"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.