spring booot "hello world"之后对spring boot的了解

一、POM文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
</parent>
这是我们表面上导入的父项目,而这个父项目仍然有项目依赖
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-dependencies</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
这个导入的父项目才是真正我们依赖的各种包的版本和内容
他来管理springboot的所有的版本依赖

springboot的仲裁中心:
这就是我们上面第二个 导入的所有依赖的版本管理信息
在这里插入图片描述
以后我们导入的依赖是不需要声明版本的,(当然不存在dependencies管理的依赖仍然是需要我们来指定版本号)
二、导入的依赖

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

spring-boot-starter:spring-boot的场景启动器,他帮我们导入了我们web正常运行所依赖的所有的组件
spring boot 将所有的功能场景全部抽取出来了,做成了一个个的starters启动器,我们只需要在项目中导入相应starters的场景,所有的依赖我们就导入进来了,

三、主程序类
@SpringBootApplication
以上的注解配置在那个类上该类就是我们的主配置类,我们就应该启动这个类的主程序方法,来启动我们的springboot应用

/*
 * @SpringBootApplication来标注一个主程序类,说明这是一个spring boot应用
 *
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
//        spring boot 应用启动起来
//        SpringApplication.run(HelloWorldMainApplication.class, args);
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

点击@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}
)}
)

@SpringBootConfiguration: spring boot 的配置类
标志在某个类上,表示这是一个springboot的配置类

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

@Configuration 配置类上来标注这个注解, 配置类也是我们容器中的一个组件
@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 {};
}```

这个里边
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
就会自动给我们配置包并导入包名,将我们的主配置所在包中的所有的组件进行扫描,所以说,如果我们将我们的controller所在的包,放在了我们主配置类的外边的话,到时我们就不能扫描到,我们在网页中访问的话会包404的问题
这个注解中的EnableAutoConfigurationImportSelector:导入哪些组件的选择器
将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中




发布了12 篇原创文章 · 获赞 1 · 访问量 1583

猜你喜欢

转载自blog.csdn.net/qq_43031234/article/details/100248920