Jersey和spring集成 – bean Injections在运行时为null
我正在尝试将服务注入使用Jersey的Rest类。
无论我尝试注入此类的内容或方式,似乎都会在运行时显示为null。 查看日志文件显示在初始化Web应用程序时调用setJsonTestService,此时它不为null。 但是,当稍后通过对此类的PUT请求访问它时,它为null。
我完全不知所措。
这个类看起来像这样:
@Named @Path("JsonTest") public class JsonTest { @Context Request request; @Context UriInfo uriInfo; protected final Logger log = Logger.getLogger(getClass()); private JsonTestService jsonTestService; @Autowired public void setJsonTestSerivce(JsonTestService jsonTestService) { log.info("Setting JsonTestService."); if (jsonTestService == null) { log.info("JsonTestService is null at injection"); } this.jsonTestService = jsonTestService; } @Inject public ScrapIntermediate scrapIntermediate; // just a plain empty class with an is true method @PUT @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public void putJson(@PathParam("id") String id) { log.info("Putting some json at " + id); if (scrapIntermediate == null) { log.info("scrapIntermediate is null..."); } if (jsonTestService != null) { jsonTestService.sendUpdate(); } else { log.info("jsonTestService is null..."); } } }
有任何想法吗?
更新:
web.xml(泽西岛)
Jersey REST Service com.sun.jersey.spi.spring.container.servlet.ServletContainer com.sun.jersey.config.property.packages EDAS 1 Jersey REST Service /rest/*
尝试
com.sun.jersey.spi.spring.container.servlet.SpringServlet
您还需要contextConfigLocation
,但我假设您拥有它。 有关设置的详细信息, 请参见此处
正如亚光b建议spring实例化你的对象,但泽西岛对弹簧一无所知并再次实例化它们。 当您使用SpringServlet
它应该找到spring应用程序上下文。
也就是说,spring-mvc支持RESTful服务,这与JAX-RS非常相似。 你也可以尝试一下。