springboot系列(一)-HelloWorld入门和原理分析

本文介绍了SpringBoot HelloWorld的创建过程,从新建工程、编写主启动类到配置Controller进行测试。接着深入解析了@SpringBootApplication注解,包括@Configuration、@EnableAutoConfiguration及@AutoConfigurationPackage的作用,阐述了自动配置类的加载机制,涉及classpath下的spring.factories文件。
摘要由CSDN通过智能技术生成

一、SpringBoot Helloworld

1、功能说明

发送一个请求,返回一个结果,最基本的web工程

2、搭建springboot工程

2.1、新建一个springboot-study工程

2.2、添加一个springboot-study-HelleWorld模块

2.2、在父pom里面添加依赖

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
        </dependency>
    </dependencies>

2.3、完善父、子pom文件

在父pom里面添加插件,子pom暂时不需要添加任何东西。

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

 

2.4、编写主启动类


@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class,args);
    }
}

2.5、添加配置文件application.properties

server.port=8001

2.6、写一个controller进行测试

@RestController
public class HelloWorldController {

    @RequestMapping(value = "/helloworld/test",method = RequestMethod.GET)
    public String test(@RequestParam("id") String id) {
        System.out.println("id:"+id);
        return "helloworld"+id;
    }
}

2.7、测试

启动:com.java.code.study.HelloWorldApplication

浏览器输入:http://localhost:8001/helloworld/test?id=springboot

二、利用springboot-study-Helloworld进行原理

1、主程序类

@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class,args);
    }
}

2、@SpringBootApplication表示这个类是启动类,具体如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    ...
}

3、先分析@SpringBootConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

通过注解可以知道@SpringBootConfiguration和@Configuration作用一样,表示是一个配置类,也就是我们的主启动类是一个配置类会被注入到spring容器里面。

4、再分析@EnableAutoConfiguration注解

回到@SpringBootApplication注解里面还有一个@EnableAutoConfiguration注解,看一下具体的定义:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

5、分析@EnableAutoConfiguration里面的@AutoConfigurationPackage注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
        Registrar() {
        }

        public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
            AutoConfigurationPackages.register(registry, (new AutoConfigurationPackages.PackageImport(metadata)).getPackageName());
        }

        public Set<Object> determineImports(AnnotationMetadata metadata) {
            return Collections.singleton(new AutoConfigurationPackages.PackageImport(metadata));
        }
    }

重点看一下org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar#registerBeanDefinitions方法,这个方法是Spring里面的方法,接着会调用里面的register方法,register方法把第二个参数代表的包里面的bean注入到spring容器里面,可以看一下把哪些包注入进去呐,可以采用debug方式启动一下看一下第二个参数的值。我这边就是:com.java.code.study。说明@AutoConfigurationPackage注解会把启动类所在包以及子包里面的bean进行扫描注册,注入的bean需要加注入的注解。

6、分析@EnableAutoConfiguration里面的@Import({EnableAutoConfigurationImportSelector.class})

这个注解注意注入EnableAutoConfigurationImportSelector类所以看一下这个类里面的内容,在它的父类里面有一个方法:

public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            try {
                AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
                AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
                List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
                configurations = this.removeDuplicates(configurations);
                configurations = this.sort(configurations, autoConfigurationMetadata);
                Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
                this.checkExcludedClasses(configurations, exclusions);
                configurations.removeAll(exclusions);
                configurations = this.filter(configurations, autoConfigurationMetadata);
                this.fireAutoConfigurationImportEvents(configurations, exclusions);
                return (String[])configurations.toArray(new String[configurations.size()]);
            } catch (IOException var6) {
                throw new IllegalStateException(var6);
            }
        }
    }

这个方法会返回大量的自动配置类然后进行注入,重点是这些自动配置类是怎么获取的呐,主要看下面方法实现:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

接着再看里面loadFactoryNames方法的实现:

public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
        String factoryClassName = factoryClass.getName();

        try {
            Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
            ArrayList result = new ArrayList();

            while(urls.hasMoreElements()) {
                URL url = (URL)urls.nextElement();
                Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
                String factoryClassNames = properties.getProperty(factoryClassName);
                result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
            }

            return result;
        } catch (IOException var8) {
            throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + "META-INF/spring.factories" + "]", var8);
        }
    }

 

通过上面的代码可以知道会到classpath目录下加载META-INF/spring.factories文件,把这些文件里面的内容加载成properties对象。获取properties对象里面key为factoryClass全类名对应的值进行返回。回到上一个方法看一下factoryClass值是什么:

protected Class<?> getSpringFactoriesLoaderFactoryClass() {
        return EnableAutoConfiguration.class;
    }
//org.springframework.boot.autoconfigure.EnableAutoConfiguration

所以下面我们就到classpath下面的META-INF/spring.factories文件文件里面找key为:org.springframework.boot.autoconfigure.EnableAutoConfiguration对应的值:

例如:

org/springframework/boot/spring-boot-autoconfigure/1.5.10.RELEASE/spring-boot-autoconfigure-1.5.10.RELEASE.jar!/META-INF/spring.factories截取部分内容。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

大家可以到spring.factories里面查看一下,有非常多类,那么这些类都会加载进去吗?哪些加载?怎么加载?和我们开发自己配置的值怎么进行交互等问题会在后续讲解。

代码源码:https://github.com/spingcode/springboot-study/tree/master

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值