设为首页 加入收藏

TOP

@Import与@ImportResource注解的解读(一)
2019-09-03 03:40:11 】 浏览:194
Tags:@Import @ImportResource 注解 解读

前言


在使用Spring-Cloud微服务框架的时候,对于@Import和@ImportResource这两个注解想必大家并不陌生。我们会经常用@Import来导入配置类或者导入一个带有@Component等注解要放入Spring容器中的类;用@ImportResource来导入一个传统的xml配置文件。另外,在启用很多组件时,我们会用到一个形如@EnableXXX的注解,比如@EnableAsync、@EnableHystrix、@EnableApollo等,点开这些注解往里追溯,你也会发现@Import的身影。如此看来,这两个注解与我们平时的开发关系密切,但大家知道它们是如何发挥作用的吗?下面就一起探索一下。


正文


首先看这两个注解的路径,它们都位于org.springframework.context.annotation包下,可以说是根正苗红的Spring注解,所以对这两个注解的处理,更多的也是在原有的Spring框架中进行的。在Spring-Cloud启动类的run方法中,通过简单的追溯我们可以定位到这个run方法(仅部分代码):


public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();


        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();


可以看到,在19行的位置,调用 refreshContext方法,看到这里,想必都会想到Spring中大名鼎鼎的refresh方法,确实如此,正是在这个方法里面完成了对refresh方法的调用。对这两个注解的处理,应该还是落在refresh方法中。


protected ConfigurableApplicationContext createApplicationContext() {
        Class<?> contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                switch(this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
                    break;
                case REACTIVE:
                    contextClass = Class.forName("org.springframework.boot.web.re

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring源码解读之BeanFactoryPost.. 下一篇Thread线程的基础知识及常见疑惑点

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目