zoukankan      html  css  js  c++  java
  • 【Spring boot】SpringApplication三板斧

    SpringBoot程序启动入口的核心注解@SpringBootApplication,这个注解是由三部分组成:

    1.  @SpringBootConfiguration,这个注解主要是继承@Configuration注解,主要用于加载配置文件。
    2.  @ComponentScan,主要用于组件扫描和自动装配。
    3.  @EnableAutoConfiguration,这个注释启用了Spring Boot的自动配置功能,可以自动为您配置很多东西。

    一、@EnableAutoConfiguration

    作用

    启用了Spring Boot的自动配置功能,根据你pom.xml引入的xxx-boot-starter可以自动为配置很多东西。

    流程图

    • 我们引入starter的依赖,会将自动配置的类的jar引入。
    • @SpringBootApplication的注解中有一个是@EnableAutoConfiguration注解,这个注解有一个@Import({EnableAutoConfigurationImportSelector.class}),
    • EnableAutoConfigurationImportSelector内部则是使用了SpringFactoriesLoader.loadFactoryNames方法进行扫描具有META-INF/spring.factories文件的jar包。
    • 应用在启动时就会加载spring-boot-autoconfigure的jar包下面的META-INF/spring.factories文件中定义的autoconfiguration类。
    • 将configuration类中定义的bean加入spring到容器中。就相当于加载之前我们自己配置组件的xml文件。而现在SpringBoot自己定义了一个默认的值,然后直接加载进入了Spring容器。
    • spring-boot-autoconfigure的jar包里面通过condition的方式判断是否将xxxx-starter(比如aop、jdbc。。。)初始化。(pom.xml引入了jar包才能初始化)。

    二、@ComponentScan

    作用

    根据定义的扫描路径,把符合扫描规则的类装配到spring的bean容器中。

    • 自定扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入spring容器
    • 通过includeFilters加入扫描路径下没有以上注解的类加入spring容器
    • 通过excludeFilters过滤出不用加入spring容器的类
    • 自定义增加了@Component注解的注解方式

    使用方法

    basePackages与value: 用于指定包的路径,进行扫描

    @ComponentScan(basePackages = “”)     //单个
    @ComponentScan(basePackages = {“qqq”,“www”,“…”})   //多个
    @ComponentScan("com.baker.learning.*")   //通配符匹配所有的包

    basePackageClasses: 用于指定某个类的包的路径进行扫描

    @ComponentScan(basePackageClasses = “”)   //单个
    @ComponentScan(basePackageClasses = {“qqq”,“www”,“…”})  //多个

    nameGenerator: bean的名称的生成器

    useDefaultFilters: 是否开启对@Component,@Repository,@Service,@Controller的类进行检测

    includeFilters: 包含的过滤条件 FilterType.ANNOTATION:按照注解过滤,FilterType.ASSIGNABLE_TYPE:按照给定的类型,FilterType.ASPECTJ:使用ASPECTJ表达式,FilterType.REGEX:正则,FilterType.CUSTOM:自定义规则

    excludeFilters: 排除的过滤条件,用法和includeFilters一样

    scopedProxy

    resourcePattern

    scopeResolver

    三、@SpringBootConfiguration

    作用

    SpringBootConfiguration是一个组合注解,SpringBootConfiguration注解可以实现spring中xml配置文件配置的效果,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。(也可以重新指定)。

    @SpringBootConfiguration
    public class RestTemplateConfig {
        /**
         * 设置
         *
         * @param factory
         * @return
         */
        @Bean
        public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory factory) {
            return new RestTemplate(factory);
        }
    
        @Bean
        public HttpComponentsClientHttpRequestFactory simpleClientHttpRequestFactory() {
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
            //达到最大请求时间,请求将自动视为失败,结束当前进程。
            factory.setReadTimeout(20000);  //单位为ms
            factory.setConnectTimeout(20000); //单位为ms
            return factory;
        }
    
    }
  • 相关阅读:
    NIO与普通IO文件读写性能对比
    JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析
    软件工程之软件设计
    ubuntu下管理android手机
    AFNetworking2.0 NSHipster翻译
    【Jsoup爬取网页内容】
    IOS 表视图UITableView 束NSBundle
    如何将位图格式图片文件(.bmp)生成geotiff格式图片?
    opencv3 使用glob遍历并修改文件名
    Ubuntu clion下载及激活
  • 原文地址:https://www.cnblogs.com/kbian/p/12804732.html
Copyright © 2011-2022 走看看