springboot情操陶冶-web配置(一)

在pom.xml文件中引入web依赖,炒鸡简单,如下

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

上述的三行依赖代码便完成了对web环境的配置,此时可以直接运行main()方法

package com.example.demospringbootweb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoSpringbootWebApplication { public static void main(String[] args) { SpringApplication.run(DemoSpringbootWebApplication.class, args); } }

默认服务是挂载在Tomcat容器中,端口为8080。所以可以通过该链接直接访问:8080便可得到以下页面(未配置index页面的效果)

404_page

应用端口和上下文配置

本文将在上文的基础山讲解端口和上下文路径的具体配置以及解析。现附上简单的步骤操作

创建application-servlet.properties文件,专门用于配置应用服务

#server application config server.port=9001 server.servlet.context-path=http://www.likecs.com/demoWeb

在application.properties文件中指定激活的profile,用于使上述文件生效

spring.profiles.active=servlet

为了使界面变得稍微友好,引入index.html文件,放置于static目录下,如下

static_resource

继续运行对应的main()函数,便可访问:9001/demoWeb,得到以下结果

index_page

源码剖析

关于Tomcat等容器的配置,springboot采用了EmbeddedWebServerFactoryCustomizerAutoConfigurationServletWebServerFactoryAutoConfiguration两个类便完成了。笔者针对这两个类进行简单的分析

EmbeddedWebServerFactoryCustomizerAutoConfiguration

直接查看其内部源码,如下

@Configuration @EnableConfigurationProperties(ServerProperties.class) public class EmbeddedWebServerFactoryCustomizerAutoConfiguration { }

主要是引入了ServerProperties配置类,而其是读取spring上下文环境中的以server为开头的属性,简单的看下

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties { @ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class }) public static class TomcatWebServerFactoryCustomizerConfiguration { @Bean public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer( Environment environment, ServerProperties serverProperties) { return new TomcatWebServerFactoryCustomizer(environment, serverProperties); } } /** * Nested configuration if Jetty is being used. */ @Configuration @ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class }) public static class JettyWebServerFactoryCustomizerConfiguration { @Bean public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer( Environment environment, ServerProperties serverProperties) { return new JettyWebServerFactoryCustomizer(environment, serverProperties); } } /** * Nested configuration if Undertow is being used. */ @Configuration @ConditionalOnClass({ Undertow.class, SslClientAuthMode.class }) public static class UndertowWebServerFactoryCustomizerConfiguration { @Bean public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer( Environment environment, ServerProperties serverProperties) { return new UndertowWebServerFactoryCustomizer(environment, serverProperties); } } }

样例中的port/servlet.context-path便是保存在ServerProperties对象中的,具体其内部的属性本文就不展开了,读者可自行去阅读源码。
由上述的简单代码得知该自动配置类主要根据classpath环境创建不同的应用容器,默认springboot集成的都是tomcat。我们此处只关注下TomcatWebServerFactoryCustomizer类,下文中会有所提及

ServletWebServerFactoryAutoConfiguration

具体的ServletWebServer容器配置是通过ServletWebServerFactoryAutoConfiguration来创建的,由于代码过长笔者分为几个部分来讲解

头上注解先瞧一发

@Configuration @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @ConditionalOnClass(ServletRequest.class) @ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties(ServerProperties.class) @Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class }) public class ServletWebServerFactoryAutoConfiguration { }

要想本自动配置生效则必须classpath环境中存在ServletRequest.class等servlet环境依赖类,这一般引入开头的starter-web版块便基本满足了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywgpg.html