【发布时间】:2015-10-18 08:47:31
【问题描述】:
我正在使用 Embedded Jetty 和 Spring MVC,并且我正在通过 main 函数启动我的 WebApplication。 我不喜欢它的外观,它不是很干净,我觉得我做错了什么,即使它工作得很好。 我的问题是我想在 main 方法之外初始化 ServerHolder、ServletContextHandler 和 Server 对象,但我不能这样做,因为 DispatcherServlet 需要和 ApplicationContext。
有什么想法吗?
public static void main(String[] args) throws Exception {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(EPConfiguration.class);
ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(applicationContext));
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
BasicConfigurator.configure();
Server server = new Server();
server.setHandler(context);
//HTTP
ServerConnector connector = new ServerConnector(server);
connector.setPort(9999);
server.setConnectors(new Connector[]{connector});
server.start();
server.join();
}
【问题讨论】: