我使用Grizzly作为Web服务器来创建与Jersey的独立REST服务。我也想在应用程序中使用Spring IoC,但是我遇到了自动布线bean的问题。
要创建服务器,我在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();
}
}
我的spring-context.xml如下所示:
<context:annotation-config />
<context:component-scan base-package="com.test"/>
我想在Jersey资源类中自动连接BO对象:
@Path("/test")
@Service
public class Test {
@Autowired
private testBO testBo;
....
@GET
@Path("/info")
public String getInfo() {
return testBo.get();
}
}
现在,每当我在Test类中调用一个引用testBo的方法时,我都会得到 NullPointerException ,我认为这意味着bean没有自动连接。
testBO类具有@Component注释。此外,所有相关的bean都是“com.test”包或其子包的一部分。
任何人都知道我做错了什么?
如下所示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)
我也在控制台输出中看到:
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
答案 0 :(得分:0)
如果你有Java 8 - 可能值得一看Microserver。
它提供了整合Grizzly,Jersey 2和Spring 4.0的基础设施。如果您的main方法在com.test包中,那么您只需要: -
public static void main(String[] args){
new MicroserverApp(()->"context").run();
}
否则,您还应该使用
注释主类@Microserver(basePackages={"com.test"})