【发布时间】:2016-03-30 20:04:09
【问题描述】:
我使用 java/jetty 自托管服务器和 jersey-2 用于 java RESTful api。 应用程序具有带有属性的 application.properties 文件。
ConfigurationProperties 类读取属性文件并将其加载到java.util.Properties 类中。
Jetty 服务器实例化是通过以下方式完成的。
// Create and register resources
final ResourceConfig resourceConfig = new ApiServiceConfig()
.register(new DependencyInjectionBinder());
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/mydomain/api");
Server jettyServer = new Server(8585);
jettyServer.setHandler(contextHandler);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(resourceConfig));
contextHandler.addServlet(jerseyServlet, "/*");
// Create web context. Can't use.
//WebApplicationContext webContext = getWebApplicationContext();
// Add web context to servlet event listener.
//contextHandler.addEventListener(new ContextLoaderListener(webContext));
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
jettyServer.destroy();
}
我不能使用 Spring AnnotationConfigWebApplicationContext,因为它需要在 java-8 中不起作用的 commons-logging 依赖项。
如何使用码头/球衣上下文注册属性以及如何在以后检索值(例如:context.getProperty("prop.name"))?
【问题讨论】:
标签: java java-8 jersey-2.0 jetty-9