下面列出了org.springframework.boot.web.servlet.ServletRegistrationBean#setLoadOnStartup ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected ServletRegistrationBean registerServlet(FlowableServlet servletProperties, Class<?> baseConfig) {
AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
dispatcherServletConfiguration.setParent(applicationContext);
dispatcherServletConfiguration.register(baseConfig);
DispatcherServlet servlet = new DispatcherServlet(dispatcherServletConfiguration);
String path = servletProperties.getPath();
String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, urlMapping);
registrationBean.setName(servletProperties.getName());
registrationBean.setLoadOnStartup(servletProperties.getLoadOnStartup());
registrationBean.setAsyncSupported(true);
if (multipartConfigElement != null) {
registrationBean.setMultipartConfig(multipartConfigElement);
}
return registrationBean;
}
@Bean
ServletRegistrationBean servletRegistrationBean(ServletMappingConfiguration config) {
ServletRegistrationBean mapping = new ServletRegistrationBean();
mapping.setServlet(new CamelHttpTransportServlet());
mapping.addUrlMappings(config.getContextPath());
mapping.setName(config.getServletName());
mapping.setLoadOnStartup(1);
return mapping;
}
@Bean
ServletRegistrationBean<HttpServlet30Dispatcher> keycloakJaxRsApplication(
KeycloakServerProperties keycloakServerProperties, DataSource dataSource) throws Exception {
mockJndiEnvironment(dataSource);
EmbeddedKeycloakApplication.keycloakServerProperties = keycloakServerProperties;
ServletRegistrationBean<HttpServlet30Dispatcher> servlet = new ServletRegistrationBean<>(
new HttpServlet30Dispatcher());
servlet.addInitParameter("javax.ws.rs.Application", EmbeddedKeycloakApplication.class.getName());
servlet.addInitParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX,
keycloakServerProperties.getContextPath());
servlet.addInitParameter(ResteasyContextParameters.RESTEASY_USE_CONTAINER_FORM_PARAMS, "true");
servlet.addUrlMappings(keycloakServerProperties.getContextPath() + "/*");
servlet.setLoadOnStartup(1);
servlet.setAsyncSupported(true);
return servlet;
}
/**
* This bean registers the {@link FacesServlet}.
* <p>
* This {@link ServletRegistrationBean} also sets two
* {@link ServletContext#setAttribute(String, Object) servlet-context attributes} to inform Mojarra and MyFaces about
* the dynamically added Servlet.
*
* @param facesServletProperties The properties for the {@link FacesServlet}-registration.
*
* @return A custom {@link ServletRegistrationBean} which registers the {@link FacesServlet}.
*/
@Bean
public ServletRegistrationBean<FacesServlet> facesServletRegistrationBean(
FacesServletProperties facesServletProperties
) {
ServletRegistrationBean<FacesServlet> facesServletServletRegistrationBean = new ServletRegistrationBean<FacesServlet>(new FacesServlet()) {
@Override
protected ServletRegistration.Dynamic addRegistration(String description, ServletContext servletContext) {
ServletRegistration.Dynamic servletRegistration = super.addRegistration(description, servletContext);
if (servletRegistration != null) {
servletContext.setAttribute("org.apache.myfaces.DYNAMICALLY_ADDED_FACES_SERVLET", true);
servletContext.setAttribute("com.sun.faces.facesInitializerMappingsAdded", true);
}
return servletRegistration;
}
};
facesServletServletRegistrationBean.setName(facesServletProperties.getName());
facesServletServletRegistrationBean.setUrlMappings(facesServletProperties.getUrlMappings());
facesServletServletRegistrationBean.setLoadOnStartup(facesServletProperties.getLoadOnStartup());
facesServletServletRegistrationBean.setEnabled(facesServletProperties.isEnabled());
facesServletServletRegistrationBean.setAsyncSupported(facesServletProperties.isAsyncSupported());
facesServletServletRegistrationBean.setOrder(facesServletProperties.getOrder());
return facesServletServletRegistrationBean;
}
@Bean(name = "hystrixRegistrationBean")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/hystrix.stream");
registration.setName("hystrixServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
/**
*
* 地址/hystrix
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); //Spring boot 以代码的方式装载servlet
registrationBean.setLoadOnStartup(1); //设置启动顺序
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
System.out.println("starts server");
return registrationBean;
}
@Bean("gitServletBean")
public ServletRegistrationBean<GitServlet> gitServletBean(Path pluginDir, PluginRepoResolver pluginRepoResolver) {
GitServlet servlet = new GitServlet();
servlet.setRepositoryResolver(pluginRepoResolver);
ServletRegistrationBean<GitServlet> bean = new ServletRegistrationBean<>(servlet, GIT_URL + "/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("base-path", pluginDir.toString());
bean.addInitParameter("export-all", "true");
bean.setAsyncSupported(true);
return bean;
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
@Bean(name = "hystrixForTurbineRegistrationBean")
public ServletRegistrationBean servletTurbineRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
registration.setName("hystrixForTurbineServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
System.out.println("starts server");
return registrationBean;
}
@Bean
public ServletRegistrationBean apiDispatcher() {
DispatcherServlet api = new DispatcherServlet();
api.setContextClass(AnnotationConfigWebApplicationContext.class);
api.setContextConfigLocation(ApiDispatcherServletConfiguration.class.getName());
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(api);
registrationBean.addUrlMappings("/api/*"); // api下面的所有内容都访问到这里
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
registrationBean.setName("api"); // 不能重复,重复则以最后一个设置的为准
return registrationBean;
}
@Bean(name = "hystrixForTurbineRegistrationBean")
public ServletRegistrationBean servletTurbineRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
registration.setName("hystrixForTurbineServlet");
registration.setLoadOnStartup(1);
return registration;
}
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
System.out.println("starts server");
return registrationBean;
}
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
System.out.println("starts server");
return registrationBean;
}
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction );
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
System.out.println("starts server");
return registrationBean;
}
@Bean(name = "hystrixRegistrationBean")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/hystrix.stream");
registration.setName("hystrixServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Bean
public ServletRegistrationBean repositoryServletRegistrationBean(
RepositoryHttpServlet repositoryHttpServlet) {
ServletRegistrationBean servletRegistrationBean =
new ServletRegistrationBean(repositoryHttpServlet, "/repository_servlet/*");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}