Servlet on Springboot
组件扫描:ServletComponentScan
熟悉波~ 是不是应用跟Springboot的@ComponentScan如出一辙
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ServletComponentScanRegistrar.class)
public @interface ServletComponentScan {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
* declarations e.g.: {@code @ServletComponentScan("org.my.pkg")} instead of
* {@code @ServletComponentScan(basePackages="org.my.pkg")}.
* @return the base packages to scan
*/
@AliasFor("basePackages")
String[] value() default {};
/**
* Base packages to scan for annotated servlet components. {@link #value()} is an
* alias for (and mutually exclusive with) this attribute.
* <p>
* Use {@link #basePackageClasses()} for a type-safe alternative to String-based
* package names.
* @return the base packages to scan
*/
@AliasFor("value")
String[] basePackages() default {};
/**
* Type-safe alternative to {@link #basePackages()} for specifying the packages to
* scan for annotated servlet components. The package of each class specified will be
* scanned.
* @return classes from the base packages to scan
*/
Class<?>[] basePackageClasses() default {};
}
配置声明:@interface 暴露SpringBean @Bean
事件:Event
filter:
webFilter
OncePerRequestFilter:只调用一次且是线程安全的
而其子类得ApplicationContextHeaderFilter调用的dofilter方法就是我们上面提到的真正在请求中执行的filter
激活Springbootweb
文无第一,武无第二,没有最好的技术框架或体系,只有最适合当下业务的框架或体系
谈谈你对技术的理解:天上飞的理念,必定有落地的实现
组装SpringApplicationBuilder
你看看这名字就知道他以后干啥的,并且它包含了太多太多的东西,
SpringApplication和ApplicationContext实例的生成器,基本包含了所有的SpringbootApplacation特性
private final SpringApplication application;
private ConfigurableApplicationContext context;
private SpringApplicationBuilder parent;
AtomicBoolean是不是的看看
private final AtomicBoolean running = new AtomicBoolean(false);
private final Set<Class<?>> sources = new LinkedHashSet<>();
private final Map<String, Object> defaultProperties = new LinkedHashMap<>();
private ConfigurableEnvironment environment;
private Set<String> additionalProfiles = new LinkedHashSet<>();
private boolean registerShutdownHookApplied;
private boolean configuredAsChild = false;
@SpringBootApplication
public class TestProfiles {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestProfiles.class)
.properties("spring.config.location=classpath:/test-profiles.yml")
.properties("spring.profiles.active=oracle")
.run(args);
// 输出变量
System.out.println(context.getEnvironment().getProperty("jdbc.driver"));
// 启动第二个Spring容器,指定端口为8848
ConfigurableApplicationContext context2 = new SpringApplicationBuilder(TestProfiles.class)
.properties("spring.config.location=classpath:/test-profiles.yml")
.properties("spring.profiles.active=mysql")
.properties("server.port=8848")
.run(args);
// 输出变量
System.out.println(context2.getEnvironment().getProperty("jdbc.driver"));
}
}
Springboot自动装配
/META-INF/spring.factories
XXXAotuConfigration
NIO不是异步IO而是非阻塞IO
java9推崇模块化
ClassLoader
public static void main(String[] args) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
while (true) {
System.out.println(contextClassLoader.getClass().getName());
ClassLoader parent = contextClassLoader.getParent();
if (parent == null) {
break;
}
}
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader.getClass().getName());
}
update_time =2022年2月14日13:48:19
传统的Servlet容器 Apache Tomcat
这里只记录了部分重要场景
包含核心组件
静态资源处理
类加载
连接器
JDBC数据源
HttpServletResponse
javax.servlet.http.HttpServletResponse
public static void main(String[] args) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
while (true) {
System.out.println(contextClassLoader.getClass().getName());
ClassLoader parent = contextClassLoader.getParent();
if (parent == null) {
break;
}
}
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader.getClass().getName());
}
除了其中的状态码之外,结合最近的测试其中的实现可具体参考
addHeader方法,getHeader方法等等
BootStrap--system---common---webapp