0

war packing

This commit is contained in:
2018-09-23 18:00:15 +03:00
parent 350e80eb6a
commit b4f4ab89c7
6 changed files with 68 additions and 156 deletions

View File

@@ -0,0 +1,27 @@
package ru.dmitriymx.skeleton.springmvc;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan("ru.dmitriymx.skeleton.springmvc")
public class SpringConfigMVC implements WebMvcConfigurer {
private static final int ONE_YEAR = 365*24*60*60;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(ONE_YEAR);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(ONE_YEAR);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(ONE_YEAR);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}

View File

@@ -1,88 +1,36 @@
package ru.dmitriymx.skeleton.springmvc;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Slf4jLog;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.net.InetSocketAddress;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
@Slf4j
@RequiredArgsConstructor
public class WebApp {
private final String host;
private final int port;
public class WebApp implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(SpringConfigMVC.class);
/**
* Данный класс является файлом настройки контекста для Spring
* однако, т.к. мы никакие бины не объявляем, то класс пустует
*/
@Configuration
@EnableWebMvc
public static class SpringConfigMVC implements WebMvcConfigurer {
}
// Dispatcher Servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
/**
* Создаем Spring-контекст
*
* @return {@link WebApplicationContext}
*/
private WebApplicationContext getWebApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringConfigMVC.class);
context.register(WebAppController.class);
// или
// context.setConfigLocation("ru.dmitriymx.skeleton.springmvc");
// в этом случае будет просканирован весь пакет на @Configuration
return context;
}
dispatcher.setInitParameter("contextClass", appContext.getClass().getName());
/**
* Подготавливаем обработчик запросов
*
* @param context Spring-контекст веб-приложения {@link WebApplicationContext}
* @return {@link ServletContextHandler}
*/
private ServletContextHandler getServletContextHandler(WebApplicationContext context) {
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(null);
contextHandler.setContextPath("/");
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
contextHandler.addEventListener(new ContextLoaderListener(context));
return contextHandler;
}
servletContext.addListener(new ContextLoaderListener(appContext));
/**
* Запуск встроенного Jetty веб-сервера
*/
private void start() {
Log.setLog(new Slf4jLog("Jetty.Logger"));
Server server = new Server(new InetSocketAddress(host, port));
server.setHandler(getServletContextHandler(getWebApplicationContext()));
try {
server.start();
server.join();
} catch (Exception e) {
log.error("Error start server", e);
}
}
// UTF-8 Charactor Filter.
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
public static void main(String[] args) {
final String host = System.getProperty("host", "127.0.0.1");
final int port = Integer.parseInt(System.getProperty("port", "8080"));
log.info("Web app listen: {}:{}", host, port);
WebApp app = new WebApp(host, port);
app.start();
filterRegistration.setInitParameter("encoding", "UTF-8");
filterRegistration.setInitParameter("forceEncoding", "true");
filterRegistration.addMappingForUrlPatterns(null, true, "/*");
}
}

View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%level] (%t) \{%logger\} %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Skeleton - Spring MVC</display-name>
</web-app>