0

我使用 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
4

1 回答 1

0

如果您有 Java 8 - 可能值得一看Microserver

它提供了集成 Grizzly、Jersey 2 和 Spring 4.0 的基础设施。如果您的主要方法在包 com.test 中,那么您只需要:-

public static void main(String[] args){

     new MicroserverApp(()->"context").run();

}

否则,您还应该使用注释您的主类

@Microserver(basePackages={"com.test"})
于 2015-03-17T23:20:53.927 回答