本文整理汇总了Java中org.springframework.boot.web.servlet.ServletContextInitializer类的典型用法代码示例。如果您正苦于以下问题:Java ServletContextInitializer类的具体用法?Java ServletContextInitializer怎么用?Java ServletContextInitializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServletContextInitializer类属于org.springframework.boot.web.servlet包,在下文中一共展示了ServletContextInitializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getEmbeddedServletContainer
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) {
ClassLoader parentClassLoader = resourceLoader != null ? resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader();
Package nettyPackage = Bootstrap.class.getPackage();
String title = nettyPackage.getImplementationTitle();
String version = nettyPackage.getImplementationVersion();
logger.info("Running with " + title + " " + version);
NettyEmbeddedContext context = new NettyEmbeddedContext(getContextPath(), new URLClassLoader(new URL[]{}, parentClassLoader), SERVER_INFO);
if (isRegisterDefaultServlet()) {
logger.warn("This container does not support a default servlet");
}
/*if (isRegisterJspServlet()) {
logger.warn("This container does not support a JSP servlet");
}*/
for (ServletContextInitializer initializer : initializers) {
try {
initializer.onStartup(context);
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
int port = getPort() > 0 ? getPort() : new Random().nextInt(65535 - 1024) + 1024;
InetSocketAddress address = new InetSocketAddress(port);
logger.info("Server initialized with port: " + port);
return new NettyEmbeddedServletContainer(address, context);
}
示例2: getEmbeddedServletContainer
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatEmbeddedServletContainer(tomcat);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:20,代码来源:TomcatEmbeddedServletContainerFactory.java
示例3: configureContext
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
/**
* Configure the Tomcat {@link Context}.
* @param context the Tomcat context
* @param initializers initializers to apply
*/
protected void configureContext(Context context,
ServletContextInitializer[] initializers) {
TomcatStarter starter = new TomcatStarter(initializers);
if (context instanceof TomcatEmbeddedContext) {
// Should be true
((TomcatEmbeddedContext) context).setStarter(starter);
}
context.addServletContainerInitializer(starter, NO_CLASSES);
for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) {
context.addLifecycleListener(lifecycleListener);
}
for (Valve valve : this.contextValves) {
context.getPipeline().addValve(valve);
}
for (ErrorPage errorPage : getErrorPages()) {
new TomcatErrorPage(errorPage).addToContext(context);
}
for (MimeMappings.Mapping mapping : getMimeMappings()) {
context.addMimeMapping(mapping.getExtension(), mapping.getMimeType());
}
configureSession(context);
for (TomcatContextCustomizer customizer : this.tomcatContextCustomizers) {
customizer.customize(context);
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:31,代码来源:TomcatEmbeddedServletContainerFactory.java
示例4: onStartup
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext)
throws ServletException {
try {
for (ServletContextInitializer initializer : this.initializers) {
initializer.onStartup(servletContext);
}
}
catch (Exception ex) {
this.startUpException = ex;
// Prevent Tomcat from logging and re-throwing when we know we can
// deal with it in the main thread, but log for information here.
if (logger.isErrorEnabled()) {
logger.error("Error starting Tomcat context. Exception: "
+ ex.getClass().getName() + ". Message: " + ex.getMessage());
}
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:19,代码来源:TomcatStarter.java
示例5: getEmbeddedServletContainer
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
JettyEmbeddedWebAppContext context = new JettyEmbeddedWebAppContext();
int port = (getPort() >= 0 ? getPort() : 0);
InetSocketAddress address = new InetSocketAddress(getAddress(), port);
Server server = createServer(address);
configureWebAppContext(context, initializers);
server.setHandler(addHandlerWrappers(context));
this.logger.info("Server initialized with port: " + port);
if (getSsl() != null && getSsl().isEnabled()) {
SslContextFactory sslContextFactory = new SslContextFactory();
configureSsl(sslContextFactory, getSsl());
AbstractConnector connector = getSslServerConnectorFactory()
.getConnector(server, sslContextFactory, port);
server.setConnectors(new Connector[] { connector });
}
for (JettyServerCustomizer customizer : getServerCustomizers()) {
customizer.customize(server);
}
if (this.useForwardHeaders) {
new ForwardHeadersCustomizer().customize(server);
}
return getJettyEmbeddedServletContainer(server);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:26,代码来源:JettyEmbeddedServletContainerFactory.java
示例6: configureWebAppContext
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
/**
* Configure the given Jetty {@link WebAppContext} for use.
* @param context the context to configure
* @param initializers the set of initializers to apply
*/
protected final void configureWebAppContext(WebAppContext context,
ServletContextInitializer... initializers) {
Assert.notNull(context, "Context must not be null");
context.setTempDirectory(getTempDirectory());
if (this.resourceLoader != null) {
context.setClassLoader(this.resourceLoader.getClassLoader());
}
String contextPath = getContextPath();
context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
context.setDisplayName(getDisplayName());
configureDocumentRoot(context);
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);
}
if (shouldRegisterJspServlet()) {
addJspServlet(context);
context.addBean(new JasperInitializer(context), true);
}
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
Configuration[] configurations = getWebAppContextConfigurations(context,
initializersToUse);
context.setConfigurations(configurations);
configureSession(context);
postProcessWebAppContext(context);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:31,代码来源:JettyEmbeddedServletContainerFactory.java
示例7: startBlocksUntilReadyToServe
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void startBlocksUntilReadyToServe() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
final Date[] date = new Date[1];
this.container = factory
.getEmbeddedServletContainer(new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
try {
Thread.sleep(500);
date[0] = new Date();
}
catch (InterruptedException ex) {
throw new ServletException(ex);
}
}
});
this.container.start();
assertThat(date[0]).isNotNull();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:22,代码来源:AbstractEmbeddedServletContainerFactoryTests.java
示例8: loadOnStartAfterContextIsInitialized
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void loadOnStartAfterContextIsInitialized() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
final InitCountingServlet servlet = new InitCountingServlet();
this.container = factory
.getEmbeddedServletContainer(new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
servletContext.addServlet("test", servlet).setLoadOnStartup(1);
}
});
assertThat(servlet.getInitCount()).isEqualTo(0);
this.container.start();
assertThat(servlet.getInitCount()).isEqualTo(1);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:AbstractEmbeddedServletContainerFactoryTests.java
示例9: multipleConfigurations
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void multipleConfigurations() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
ServletContextInitializer[] initializers = new ServletContextInitializer[6];
for (int i = 0; i < initializers.length; i++) {
initializers[i] = mock(ServletContextInitializer.class);
}
factory.setInitializers(Arrays.asList(initializers[2], initializers[3]));
factory.addInitializers(initializers[4], initializers[5]);
this.container = factory.getEmbeddedServletContainer(initializers[0],
initializers[1]);
this.container.start();
InOrder ordered = inOrder((Object[]) initializers);
for (ServletContextInitializer initializer : initializers) {
ordered.verify(initializer).onStartup((ServletContext) anyObject());
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:18,代码来源:AbstractEmbeddedServletContainerFactoryTests.java
示例10: rootServletContextResource
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void rootServletContextResource() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
final AtomicReference<URL> rootResource = new AtomicReference<URL>();
this.container = factory
.getEmbeddedServletContainer(new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
try {
rootResource.set(servletContext.getResource("/"));
}
catch (MalformedURLException ex) {
throw new ServletException(ex);
}
}
});
this.container.start();
assertThat(rootResource.get()).isNotNull();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:21,代码来源:AbstractEmbeddedServletContainerFactoryTests.java
示例11: sessionServletRegistration
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
protected final ServletContextInitializer sessionServletRegistration() {
ServletRegistrationBean bean = new ServletRegistrationBean(new ExampleServlet() {
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
HttpSession session = ((HttpServletRequest) request).getSession(true);
long value = System.currentTimeMillis();
Object existing = session.getAttribute("boot");
session.setAttribute("boot", value);
PrintWriter writer = response.getWriter();
writer.append(String.valueOf(existing) + ":" + value);
}
}, "/session");
bean.setName("session");
return bean;
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:19,代码来源:AbstractEmbeddedServletContainerFactoryTests.java
示例12: servletContextInitializerBeans
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void servletContextInitializerBeans() throws Exception {
addEmbeddedServletContainerFactoryBean();
ServletContextInitializer initializer1 = mock(ServletContextInitializer.class,
withSettings().extraInterfaces(Ordered.class));
given(((Ordered) initializer1).getOrder()).willReturn(1);
ServletContextInitializer initializer2 = mock(ServletContextInitializer.class,
withSettings().extraInterfaces(Ordered.class));
given(((Ordered) initializer2).getOrder()).willReturn(2);
this.context.registerBeanDefinition("initializerBean2",
beanDefinition(initializer2));
this.context.registerBeanDefinition("initializerBean1",
beanDefinition(initializer1));
this.context.refresh();
ServletContext servletContext = getEmbeddedServletContainerFactory()
.getServletContext();
InOrder ordered = inOrder(initializer1, initializer2);
ordered.verify(initializer1).onStartup(servletContext);
ordered.verify(initializer2).onStartup(servletContext);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:21,代码来源:EmbeddedWebApplicationContextTests.java
示例13: servletContextInitializerBeansDoesNotSkipServletsAndFilters
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Test
public void servletContextInitializerBeansDoesNotSkipServletsAndFilters()
throws Exception {
addEmbeddedServletContainerFactoryBean();
ServletContextInitializer initializer = mock(ServletContextInitializer.class);
Servlet servlet = mock(Servlet.class);
Filter filter = mock(Filter.class);
this.context.registerBeanDefinition("initializerBean",
beanDefinition(initializer));
this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
this.context.refresh();
ServletContext servletContext = getEmbeddedServletContainerFactory()
.getServletContext();
verify(initializer).onStartup(servletContext);
verify(servletContext).addServlet(anyString(), (Servlet) anyObject());
verify(servletContext).addFilter(anyString(), (Filter) anyObject());
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:19,代码来源:EmbeddedWebApplicationContextTests.java
示例14: getEmbeddedServletContainer
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
tomcat.getEngine().setBackgroundProcessorDelay(-1);
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatEmbeddedServletContainer(tomcat);
}
示例15: configureWebAppContext
import org.springframework.boot.web.servlet.ServletContextInitializer; //导入依赖的package包/类
/**
* Configure the given Jetty {@link WebAppContext} for use.
* @param context the context to configure
* @param initializers the set of initializers to apply
*/
protected final void configureWebAppContext(WebAppContext context,
ServletContextInitializer... initializers) {
Assert.notNull(context, "Context must not be null");
context.setTempDirectory(getTempDirectory());
if (this.resourceLoader != null) {
context.setClassLoader(this.resourceLoader.getClassLoader());
}
String contextPath = getContextPath();
context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
context.setDisplayName(getDisplayName());
configureDocumentRoot(context);
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);
}
if (shouldRegisterJspServlet()) {
addJspServlet(context);
}
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
Configuration[] configurations = getWebAppContextConfigurations(context,
initializersToUse);
context.setConfigurations(configurations);
configureSession(context);
postProcessWebAppContext(context);
}