zoukankan      html  css  js  c++  java
  • Spring Boot详解

    Spring Boot

    启动类注解

    @SpringBootApplication
    

    放在某个类上 说明该类是spring boot的主配置类 点开后

    @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 {}
    

    @SpringBootConfiguration( ->spring boot):

    标注在某一个类上表示 spring boot配置类

    点开 其实是一个配置类注解

    @Configuration  --->spring
    

    @EnableAutoConfiguration:开启自动配置

    ​ 以前需要配置的 spring boot 自动配置 点进来

    @AutoConfigurationPackage
    @Import(AutoConfigurationImportSelector.class)
    public @interface EnableAutoConfiguration {}
    

    @AutoConfigurationPackage:自动配置包

    @Import(AutoConfigurationImportSelector.class)

    将主配置类所在包及下面所有子包下所有组件扫描到spring容器

    spring底层注解@Import 给容器中导入一个组件


    @Import(EnableAutoConfigurationSelector.class)

    此注解可以配合@Configuration完成配置 示例代码

    package com.coding.config;
    
    import com.corundumstudio.socketio.SocketConfig;
    import com.corundumstudio.socketio.SocketIOServer;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Scope;
    
    @Slf4j
    @Configuration
    @EnableConfigurationProperties({
            AppProperties.class,
    })
    public class AppConfiguration {
    
        @Scope("prototype")
        @Bean
        public SocketIOServer socketIoServer(AppProperties appProperties) {
            SocketConfig socketConfig = new SocketConfig();
            socketConfig.setTcpNoDelay(true);
            socketConfig.setSoLinger(0);
            com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
            config.setSocketConfig(socketConfig);
            config.setHostname(appProperties.getHost());
            config.setPort(appProperties.getPort());
            config.setBossThreads(appProperties.getBossCount());
            config.setWorkerThreads(appProperties.getWorkCount());
            config.setAllowCustomRequests(appProperties.isAllowCustomRequests());
            config.setUpgradeTimeout(appProperties.getUpgradeTimeout());
            config.setPingTimeout(appProperties.getPingTimeout());
            config.setPingInterval(appProperties.getPingInterval());
            return new SocketIOServer(config);
        }
    
    }
    
    //AppProperties.class
    package com.coding.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @Data
    @Component
    @ConfigurationProperties(prefix = "ws")
    /*
    * @ConfigurationProperties告诉spring boot 将本类中所有属性和配置文件性格配置进行绑定 需要在
    * prefix = "??" (可以省略)
    *需导入配置文件处理器
    * <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
               
            </dependency>
    */
    public class AppProperties {
        /**
         * title
         */
        private String title;
    
        /**
         * host
         */
        private String host;
    
        /**
         * port
         */
        private Integer port;
    
        /**
         * bossCount
         */
        private int bossCount;
    
        /**
         * workCount
         */
        private int workCount;
    
        /**
         * allowCustomRequests
         */
        private boolean allowCustomRequests;
    
        /**
         * upgradeTimeout
         */
        private int upgradeTimeout;
    
        /**
         * pingTimeout
         */
        private int pingTimeout;
    
        /**
         * pingInterval
         */
        private int pingInterval;
    }
    //yml
    ws:
      port: 8888
      title: arduino服务器
      host: 0.0.0.0
      boss-count: 1
      work-count: 100
      allow-custom-requests: true
      upgrade-timeout: 10000
      ping-timeout: 60000
      ping-interval: 25000
    

    二、配置文件

    Spring boot 使用一个全局配置文件

    1、application.yml

    2、application.properties

    (配置文件的作用是对spring boot默认配置进行修改)

    yam - properties - xml 示例

    #yaml
    server:
    	port:8081
    
    #properties
    server.port=8081
    
    <!--xml-->
    <server>
        <port>8081</port>
    </server>
    

    yml 缩进控制 大小写敏感

    支持字面量,数组、map

    Profile切换环境

    多配置文件 application-{name}.properties/yml

    properties切换环境

    spring.profiles.active=?? 
    

    yml切换

    server:
      port: 8080
    spring:
      profiles:
        active: dev/prod
    ---
    server:
      port: 8081
    spring:
      profiles:prod
    ---
    server:
      port: 8081
    spring:
      profiles:dev
    ---
    

    有加载优先级顺序(可做互补配置)也可以把配置文件写在外部

    Spring boot 自动装配原理

    1、启动时加载主配置类 开启自动配置@EnableConfiguration

    2、@EnableConfiguration作用:

    ​ 利用EnableConfigurationImportSelector 给容器中导入组件

    ​ 从properties中获取EnableConfiguration.class(类名对应的值)

    类路径下 META-INF/spring.factories里面所有EnableConfiguration的值加入到容器中

    重点

    • spring boot 启动会加载大量自动配置类
    • 自动配置类配置了哪些组件,组件有 无需配置
    • 给容器自动装配组件时候 会从properties类中获取某些属性 就可以制定这些属性值

    xxxAutoConfigurartion:自动配置类

    给容器中添加组件

    xxxProperties:封装配置文件中相关的属性

    嵌入式Servlet容器

    后续...

  • 相关阅读:
    【矩阵】
    关于集合的思路
    TSQL生成Combguid
    ServiceLocator是反模式
    使用表达式树创建对象
    TeamCity配置笔记
    AutoMapper映射ExpressionTree
    Automapper扩展方法
    Cache&Session Viewer
    Orchard源码:Logging
  • 原文地址:https://www.cnblogs.com/laowt/p/14776564.html
Copyright © 2011-2022 走看看