springapplication注解_java导入外部jar包

springapplication注解_java导入外部jar包SpringApplication定义:Spring应用引导类,提供便利的自定义行为方法场景:嵌入式Web应用和非Web应用准备阶段配置:SpringBean来源 Java配置Class:Spring注解驱动中Java配置类,大多是情况下是Spring模式注解锁标注的类,如被@configuration标注的类 XML上下文配置文件:用于Spring传统配置驱动中的xml文件 BeanDefinitionLoader(BeanDefinitionRegistryregistr

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

SpringApplication

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

准备阶段

  • 配置:Spring Bean来源
    • Java配置Class:Spring注解驱动中Java配置类,大多是情况下是Spring 模式注解锁标注的类,如被@configuration标注的类
    • XML上下文配置文件:用于Spring 传统配置驱动中的xml文件
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
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;
	}

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

/**
 * {@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包

  • 加载:应用上下文初始化器 和 应用事件监听器
    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.net/195421.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)

    【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)

    2022年1月4日
    39
  • Anaconda清华镜像源的使用及安装Pytorch失败问题解决

    Anaconda清华镜像源的使用及安装Pytorch失败问题解决AnacondaAnaconda(官方网站)是一个用于科学计算的Python发行版,支持Linux,Mac,Windows,包含了众多流行的科学计算、数据分析的Python包。Anaconda本身也可以通过清华镜像下载。在Anaconda中使用清华镜像源1添加清华镜像至Anaconda仓库运行以下命令,将清华镜像添加至Anaconda仓库中condaconfig-…

    2022年9月28日
    0
  • 【Tensorflow】Windows安装tensorflow错误原因查询、卸载tensorflow与重新安装

    【Tensorflow】Windows安装tensorflow错误原因查询、卸载tensorflow与重新安装Windows安装tensorflow错误原因查询、卸载tensorflow与重新安装深度学习这个大坑的苦与甜,谁踩谁知道。上文介绍了windows如何安装tensorflow点我:Windows(64)位安装tensorflow,但因为一般电脑上会有好几个版本的python,比如我电脑上装了从2.7各版到3.6等等(具体怎么查询电脑的python版本下面会介绍到),所以在安装的时候一定要确认…

    2022年6月22日
    30
  • 使用c++SFML制作月圆之夜总集篇[通俗易懂]

    使用c++SFML制作月圆之夜总集篇[通俗易懂]写在开头重新以时间线的形式整理一下去年使用c++的SFML库制作月圆之夜(游戏程序设计大作业)的开发过程,括号里面是新的补充以及对一年前自己的吐槽因为是在大二转专业后做首次接触游戏开发后才做的,当时c++学习得并不好,所以代码很乱很糟糕,许多思路也不是很清晰,完全是摸爬滚打混过来的,最后也有很多bug,不过还是一次很有收获的经历当时也尝试着学习用游戏引擎做游戏,还觉得游戏引擎太难用了,现在想想游戏引擎是真的方便,真香2020年4月6日昨天做完扫雷后,思考了一下游戏程序设计的课程设计应该做什么。虽然

    2025年7月1日
    0
  • 门面模式和适配器模式_数字化门店转型

    门面模式和适配器模式_数字化门店转型门面模式Facade动机模式定义结构要点总结笔记动机上述A方案的问题在于组件的客户和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和各子系统的演化.这种过多的耦合面临很多变化的挑战如何简化外部客户端和系统间的交互接口呢?如何将外部客户程序的演化和内部子系统的变化之间的依赖相互解耦模式定义为子系统中的一组接口提供一个**一致(稳定)**的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用(复用)结构要点总结从客户程序的角度来看,Facade模式简化了整个

    2022年8月9日
    3
  • babel-preset-react_babel-loader

    babel-preset-react_babel-loaderhttps://www.fullstackreact.com/articles/what-are-babel-plugins-and-presets/当开发react或者vuejsapp时,开发者

    2022年8月2日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号