下面列出了org.springframework.boot.web.server.WebServer#org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void customize(ConfigurableApplicationContext context) {
if(context instanceof ServletWebServerApplicationContext
&& !AdminEndpointApplicationRunListener.isEmbeddedServletServer(context.getEnvironment())) {
MetaDataProvider metaDataProvider = context.getBean(MetaDataProvider.class);
WebServer webServer = new WebServer() {
@Override
public void start() throws WebServerException {
}
@Override
public void stop() throws WebServerException {
}
@Override
public int getPort() {
return metaDataProvider.getServerPort();
}
};
context.publishEvent(
new ServletWebServerInitializedEvent(
webServer,
new ServletWebServerApplicationContext())
);
}
}
/**
* Declare a Servlet-based web {@link ApplicationDsl application} that allows to configure a Spring Boot
* application using Jafu DSL and functional bean registration.
*/
public static JafuApplication webApplication(Consumer<ApplicationDsl> dsl) {
return new JafuApplication(new ApplicationDsl(dsl)) {
@Override
protected ConfigurableApplicationContext createContext() {
return new ServletWebServerApplicationContext();
}
};
}
@Test
public void applicationContextLoadsAndIsCorrectType() {
assertThat(this.applicationContext).isNotNull();
assertThat(this.applicationContext).isNotInstanceOf(WebApplicationContext.class);
assertThat(this.applicationContext).isNotInstanceOf(ServletWebServerApplicationContext.class);
}
/**
* Extracts a Tomcat {@link Connector} from Spring webapp context.
*/
public static Connector getConnector(ServletWebServerApplicationContext applicationContext) {
final TomcatWebServer container = (TomcatWebServer) applicationContext.getWebServer();
// Start the container to make sure all connectors are available.
container.start();
return container.getTomcat().getConnector();
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationContextInitializedEvent) {
ApplicationContextInitializedEvent initialized = (ApplicationContextInitializedEvent) event;
ConfigurableApplicationContext context = initialized.getApplicationContext();
if (!(context instanceof GenericApplicationContext)) {
throw new IllegalStateException("ApplicationContext must be a GenericApplicationContext");
}
if (!isEnabled(context.getEnvironment())) {
return;
}
GenericApplicationContext generic = (GenericApplicationContext) context;
ConditionService conditions = initialize(generic);
functional(generic, conditions);
apply(generic, initialized.getSpringApplication(), conditions);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
ApplicationEnvironmentPreparedEvent prepared = (ApplicationEnvironmentPreparedEvent) event;
if (!isEnabled(prepared.getEnvironment())) {
return;
}
logger.info("Preparing application context");
SpringApplication application = prepared.getSpringApplication();
findInitializers(application);
WebApplicationType type = getWebApplicationType(application, prepared.getEnvironment());
Class<?> contextType = getApplicationContextType(application);
if (type == WebApplicationType.NONE) {
if (contextType == AnnotationConfigApplicationContext.class || contextType == null) {
application.setApplicationContextClass(GenericApplicationContext.class);
}
}
else if (type == WebApplicationType.REACTIVE) {
if (contextType == AnnotationConfigReactiveWebApplicationContext.class || contextType == null) {
application.setApplicationContextClass(ReactiveWebServerApplicationContext.class);
}
}
else if (type == WebApplicationType.SERVLET) {
if (contextType == AnnotationConfigServletWebServerApplicationContext.class || contextType == null) {
application.setApplicationContextClass(ServletWebServerApplicationContext.class);
}
}
}
}
/**
* Returns a new {@link HealthChecker} that marks the server as unhealthy when Tomcat becomes unavailable.
*/
@Bean
public HealthChecker tomcatConnectorHealthChecker(ServletWebServerApplicationContext applicationContext) {
final Connector connector = getConnector(applicationContext);
return () -> connector.getState().isAvailable();
}
/**
* Returns a new {@link TomcatService} that redirects the incoming requests to the Tomcat instance
* provided by Spring Boot.
*/
@Bean
public TomcatService tomcatService(ServletWebServerApplicationContext applicationContext) {
return TomcatService.of(getConnector(applicationContext));
}