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();
}
posted @ 2020-05-11 09:36  薄荷少年郎微微凉  阅读(175)  评论(0编辑  收藏  举报