前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springapplication注解_java导入外部jar包

springapplication注解_java导入外部jar包

作者头像
全栈程序员站长
发布2022-09-30 19:00:23
1.1K0
发布2022-09-30 19:00:23
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

SpringApplication

定义:Spring应用引导类,提供便利的自定义行为方法 场景:嵌入式Web应用和非Web应用

准备阶段

  • 配置:Spring Bean来源
    • Java配置Class:Spring注解驱动中Java配置类,大多是情况下是Spring 模式注解锁标注的类,如被@configuration标注的类
    • XML上下文配置文件:用于Spring 传统配置驱动中的xml文件
代码语言:javascript
复制
BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
        Assert.notNull(registry, "Registry must not be null");
        Assert.notEmpty(sources, "Sources must not be empty");
        this.sources = sources;
        // 使用AnnotatedBeanDefinitionReader进行配置
        this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
        // 使用XmlBeanDefinitionReader进行配置
        this.xmlReader = XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null;
        this.groovyReader = this.isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null;
        this.scanner = new ClassPathBeanDefinitionScanner(registry);
        this.scanner.addExcludeFilter(new BeanDefinitionLoader.ClassExcludeFilter(sources));
    }
  • 推断:Web应用类型 和 主引导类(Main Class)
    • Web Reactive:webApplicaitonType.REACTIVE
    • Web Servlet:webApplicaitonType.SERVLET
代码语言:javascript
复制
static WebApplicationType deduceFromClasspath() {
        // WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
        // WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
        // JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
        // 如果没有上面的方式,默认使用Servlet_indicator_classes,如果共存的情况下优先使用Servlet_indicator_classes
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : SERVLET_INDICATOR_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

可以在引导类中指定引导的具体类型

代码语言:javascript
复制
/**
 * {@link SpringApplication} 引导类
 */

public class SpringApplicationBootstrap {
    public static void main(String[] args) {
        // 交给其运行的类只要上面有@SpringBootApplicat ion注解就行,不一定是主类名
        // SpringApplication.run(ApplicationConfiguration.class, args);
        HashSet<String> set = new HashSet<>();
        set.add(ApplicationConfiguration.class.getName());
        SpringApplication springApplication = new SpringApplication();
        springApplication.setSources(set);
        // 默认为Servlet类型,此处对它进行强制关闭,变成普通类型--主线程会中断[非web容器], web容器主线程会阻塞等待请求
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        ConfigurableApplicationContext context = springApplication.run(args);
        // 反射EnhancerBySpringCGLIB
        // com.SpringBoot.study.SpringApplicationBootstrap$ApplicationConfiguration$$EnhancerBySpringCGLIB$$ea8d68dc@400d912a
        System.out.println(context.getBean(ApplicationConfiguration.class));
    }
    
    @SpringBootApplication
    public static class ApplicationConfiguration{
    }
}
springapplication注解_java导入外部jar包
springapplication注解_java导入外部jar包

加载:应用上下文初始化器 和 应用事件监听器

代码语言:javascript
复制
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		this.bootstrappers = new ArrayList<>(getSpringFactoriesInstances(Bootstrapper.class));
        // 上下文初始化器
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
        // 监听器
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}
  • 上下文初始化器技术
    • 实现类:org.springframework.core.io.support.SpringFactoriesLoader
    • 配置资源:META-INF/spring.factories
    • 排序:AnnotationAwareOrder

《慕课网–深入Spring Boot2.0》

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/195421.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月10日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringApplication
    • 准备阶段
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档