zoukankan      html  css  js  c++  java
  • SpringBoot-3.初始结构

    3.初始结构

    工程结构(子类继承父类)

    spring-boot-dependencies 管理依赖的版本

    spring-boot-starter-parent 父工程

    hello-spring-boot 当前项目工程

    <!--hello-spring-boot的父工程spring-boot-starter-parent-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    <!--父工程的上一级spring-boot-dependencies 管理依赖的版本-->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
      </parent>
    

    以后导入依赖默认是不需要设置版本号,但是没有在dependencies管理的自然需要去声明版本号

    依赖导入

    <!--    springboot web项目启动依赖    -->        
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starters</artifactId>
        <version>2.2.6.RELEASE</version>
      </parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.2.6.RELEASE</version>
    

    spring-boot-starter:springboot场景启动器,帮我们导入web模块正常运行所需要的依赖

    https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#using-boot-starter

    主程序类

    @SpringBootApplication  // springboot主配置类,标注在哪个类上,表示springboot的主配置类
    public class SprintbootFristApplication {
        public static void main(String[] args) {
            SpringApplication.run(SprintbootFristApplication.class, args);
        }
    }
    
    // 进入SpringBootApplication类
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration // springboot配置类,标注在哪个类上,表示这是springboot的配置类
    @EnableAutoConfiguration // 开启自动配置功能
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    

    @SpringBootConfiguration

    // 进入@SpringBootConfiguration
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration // 底层的配置类,标注在哪个类上,表示这是配置类 配置类 = 配置文件 配置类也是一个组件
    

    自动配置

    @EnableAutoConfiguration

    以前我们需要配置的东西,springboot帮我们自动配置,@EnableAutoConfiguration告诉springboot开启自动配置功能,这样自动配置才生生效。

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage  //自动配置包
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
        String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
        Class<?>[] exclude() default {};
        String[] excludeName() default {};
    }
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import({Registrar.class})  //AutoConfigurationPackage通过import来完成
    public @interface AutoConfigurationPackage {
    }
    

    注解

    核心配置文件

    application.properties
    

    多环境下核心配置文件使用

    spring.profiles.active=dev
    

    dev配置文件属性

    #设置内嵌tomcat端口号
    server.port=9090
    #设置上下文根server.servlet.context-path=/dev
    #自定义值custom-name=lishicheng
    testa.name=a
    testa.age=1
    testb.name=b
    testb.age=2
    

    配置文件自定义配置的获取

    @Value("${custom-name}")
    private String CustomName;
    

    核心配置文件将自定义配置映射成一个对象

    @Component  //将此类交给spring容器管理; 添加 @Component 注解让 Component Scan 扫描到
    @ConfigurationProperties( prefix = "testa")  //配置模块。可以获取配置文件属性
    public class Test_a {
    
        private String name;
        private String age;
    
        public String getName(){
            return name;
        }
        public String getAge(){
            return age;
        }
    }
    
    @Autowired  //注入;因为通过@Component被加载,所以可以注入
    private Test_a testa;
    
    @GetMapping(value = "testa")
    public @ResponseBody String test(){
        return "a.name" +testa.getName()+ "a.age" +testa.getAge();
    }
    
  • 相关阅读:
    leetcode常规算法题复盘(第九期)——拼接最大数
    leetcode常规算法题复盘(第八期)——重构字符串
    leetcode常规算法题复盘(基础篇)——十大排序算法(二)
    leetcode常规算法题复盘(基础篇)——十大排序算法(一)
    leetcode常规算法题复盘(第七期)——区间和的个数(附带排序算法归纳)
    leetcode常规算法题复盘(第六期)——最小体力消耗路径
    leetcode常规算法题复盘(第五期)——八皇后
    leetcode常规算法题复盘(第四期)——接雨水
    leetcode常规算法题复盘(第三期)——Pow(x,n)(快速幂)
    leetcode常规算法题复盘(第二期)——特殊的二进制序列
  • 原文地址:https://www.cnblogs.com/orange-lsc/p/12867158.html
Copyright © 2011-2022 走看看