add: 添加两个帮助类WebFilterReg和WebServletReg

This commit is contained in:
Wendal Chen 2019-05-23 21:52:00 +08:00
parent 6e19c98b95
commit 42d6df69e7
5 changed files with 224 additions and 2 deletions

View File

@ -6,6 +6,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 配置文档自动生成
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Documented

View File

@ -3,6 +3,7 @@ package org.nutz.boot.starter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
@ -11,9 +12,9 @@ public interface WebServletFace {
String getName();
String getPathSpec();
default String[] getPathSpecs() {
return new String[] {getPathSpec()};
return new String[]{getPathSpec()};
}
Servlet getServlet();
@ -25,4 +26,16 @@ public interface WebServletFace {
default void setServletContext(ServletContext sc) {
}
default boolean isAsyncSupported() {
return false;
}
default MultipartConfigElement getMultipartConfig() {
return null;
}
default int getLoadOnStartup() {
return 1;
}
}

View File

@ -0,0 +1,104 @@
package org.nutz.boot.starter.impl;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import org.nutz.boot.starter.WebFilterFace;
/**
* 写个@IocBean工厂方法, 配置并返回本对象, 就可以了
*
*/
public class WebFilterReg implements WebFilterFace {
protected String name;
protected String[] pathSpecs;
protected Set<DispatcherType> dispatcheTypes = new HashSet<>();
protected Filter filter;
protected Map<String, String> initParameters = new HashMap<>();
protected int order;
public WebFilterReg() {
dispatcheTypes.add(DispatcherType.REQUEST);
}
public WebFilterReg(String name, Filter filter, String pathSpec) {
this();
this.name = name;
this.filter = filter;
this.pathSpecs = new String[]{pathSpec};
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getPathSpecs() {
return pathSpecs;
}
public void setPathSpecs(String[] pathSpecs) {
this.pathSpecs = pathSpecs;
}
public void setDispatcheTypes(Set<DispatcherType> dispatcheTypes) {
this.dispatcheTypes = dispatcheTypes;
}
public Set<DispatcherType> getDispatcheTypes() {
return dispatcheTypes;
}
public Filter getFilter() {
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
}
public Map<String, String> getInitParameters() {
return initParameters;
}
public void setInitParameters(Map<String, String> initParameters) {
this.initParameters = initParameters;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public EnumSet<DispatcherType> getDispatches() {
return EnumSet.copyOf(dispatcheTypes);
}
public void addInitParameters(String key, String value) {
initParameters.put(key, value);
}
@Override
public String getPathSpec() {
return pathSpecs == null || pathSpecs.length == 0 ? null : pathSpecs[0];
}
}

View File

@ -0,0 +1,95 @@
package org.nutz.boot.starter.impl;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
import org.nutz.boot.starter.WebServletFace;
public class WebServletReg implements WebServletFace {
protected String name;
protected String pathSpec;
protected Servlet servlet;
protected Map<String, String> initParameters = new HashMap<>();
protected boolean asyncSupported;
protected MultipartConfigElement multipartConfig;
protected int loadOnStartup = 1;
public WebServletReg() {
}
public WebServletReg(String name, Servlet servlet, String pathSpec) {
this.name = name;
this.servlet = servlet;
this.pathSpec = pathSpec;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPathSpec() {
return pathSpec;
}
public void setPathSpec(String pathSpec) {
this.pathSpec = pathSpec;
}
public Servlet getServlet() {
return servlet;
}
public void setServlet(Servlet servlet) {
this.servlet = servlet;
}
public Map<String, String> getInitParameters() {
return initParameters;
}
public void setInitParameters(Map<String, String> initParameters) {
this.initParameters = initParameters;
}
public void addInitParameters(String key, String value) {
initParameters.put(key, value);
}
public boolean isAsyncSupported() {
return asyncSupported;
}
public void setAsyncSupported(boolean asyncSupported) {
this.asyncSupported = asyncSupported;
}
public MultipartConfigElement getMultipartConfig() {
return multipartConfig;
}
public void setMultipartConfig(MultipartConfigElement multipartConfig) {
this.multipartConfig = multipartConfig;
}
public int getLoadOnStartup() {
return loadOnStartup;
}
public void setLoadOnStartup(int loadOnStartup) {
this.loadOnStartup = loadOnStartup;
}
}

View File

@ -7,6 +7,7 @@ import java.util.EventListener;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
@ -99,6 +100,11 @@ public class NbServletContextListener implements ServletContextListener {
dyna.addMapping(pathSpec);
}
dyna.setInitParameters(face.getInitParameters());
dyna.setAsyncSupported(face.isAsyncSupported());
MultipartConfigElement multipartConfig = face.getMultipartConfig();
if (multipartConfig != null)
dyna.setMultipartConfig(multipartConfig);
dyna.setLoadOnStartup(face.getLoadOnStartup());
});
// 注册监听器
appContext.getBeans(WebEventListenerFace.class).forEach((face) -> {