Spring Boot 20天入门(day1)

什么是Springboot

Springboot通过自动配置和启动项让开发者更快构建一个项目,解决了Spring与其他框架整合需要配置的大量参数,目的是简化新Spring应用的初始搭建以及开发过程,即**“约定大于配置”**

为什么要使用Springboot

原本的Springboot项目中,需要很多的XML文件来配置Spring,以及在整合第三方框架时,需要更多的配置文件,这样容易造成出错以及冲突。

通过Springboot,快速整合第三方框架,无需配置文件,代码变少,配置文件变少,内嵌Tomcat,使得开发者有更多的时间用在开发业务上。

Springboot启动中心

1、版本中心

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

​ Spring-boot-starter:Springboot场景启动器;帮我们导入了web模块正常运行所依赖的组件

Springboot讲所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。

2、主程序类,主入口类

@SpringBootApplication
public class Anhtom2000Application {

    public static void main(String[] args) {
        SpringApplication.run(Anhtom2000Application.class, args);
    }

}

@SpringBootApplication: Springboot应用标注在某个类上说明这个类时Springboot的主配置类,Springboot就应该运行在这个类的main方法来启动Springboot应用;

@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:Springboot的配置类;

​ 标注在某个类上,表示这是一个Springboot的配置类;

​ @Configuration:配置类上来标注这个注解;

​ 配置类 -> 配置文件;配置类也是容器中的一个组件;@Component

@EnableAutoConfiguration:开启自动配置功能;

​ 以前我们需要配置的东西,Springboot帮我们自动配置;加上这个注解,表示开启自动配置功能;这样自动配置才能生效;

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

@AutoConfigurationPackage : 自动配置包

​ @Import(AutoConfigurationImportSelector.class) :

​ Spring的底层注解,给容器重导入一个组件;导入的组件由 AutoConfigurationImportSelector.class指定

将主配置类 (@SpringBootConfiguration标注的类)以及所在包下面的所有子包里面的所有组件扫描进Spring容器中;

​ @Import(AutoConfigurationImportSelector.class);

​ 给容器中导入组件;

EnableAutoConfigurationImportSelector:导入哪些组件的选择器;

​ 将所有需要导入的组件以全类名的方式返回;这些组件会被添加到容器中;

​ 会给容器中导入非常多的自动配置类

protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
			AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return EMPTY_ENTRY;
		}
		AnnotationAttributes attributes = getAttributes(annotationMetadata);
		List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
		configurations = removeDuplicates(configurations);
		Set<String> exclusions = getExclusions(annotationMetadata, attributes);
		checkExcludedClasses(configurations, exclusions);
		configurations.removeAll(exclusions);
		configurations = filter(configurations, autoConfigurationMetadata);
		fireAutoConfigurationImportEvents(configurations, exclusions);
		return new AutoConfigurationEntry(configurations, exclusions);
	}

有了自动配置类,免去了我们手写配置注入功能的操作;

​ SpringFactoriesLoader。loadFactoryNames(EnableAutoConfiguration.class.classLoader)

Springboot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,讲这些值作为自动配置类注入到容器中,自动配置类就生效。

J2EE的所有整合配置都是Spring-boot-autofigure.jar下

使用Spring Initializer快速创建一个Springboot项目(IDEA)

IDE都支持使用spring项目创建向导快速创建一个Springboot项目

选择我们需要的模块;向导会联网创建Springboot项目

1、创建工程

1、选择jdk版本

在这里插入图片描述

2、点击next配置版本信息

在这里插入图片描述

3、点击next,选择模块

最基本的就是web了,所以我们选择web模块

在这里插入图片描述

4、点击next,确认信息无误后,生成springboot工程

在这里插入图片描述

2、resources文件夹中的目录结构(Springboot静态资源访问)

  • static:保存所有的静态资源:js css images;
  • templates:保存所有的模板页面;(Springboot默认使用内嵌的Tomcat,默认不支持JSP页面,但可以使用模板引擎(freemarker、thymeleaf));
  • application.properties : Springboot配置文件

3、配置文件

Springboot使用一个全局的配置文件,配置文件名是固定且唯一的;

1、application.properties

2、application.yml

配置文件的作用

修改Springboot应用自动配置的默认值;

application.properties

这个我就不说了,学过java io流的都知道。

application.yml

可以先看看官方网站

https://yaml.org/

YAML(YAML Ain’t Markup Language)是一个标记语言

标记语言:

​ 以前的配置文件,大多的是使用 xxx.xml文件(过多的标签 ,太过笨重);

​ YAML文件的核心:以数据为驱动

YAML 和 xml的对比:

server:
	port: 8881
<server>
	<port>8881</port>
</server>

使用配置文件注入JavaBean

首先我们创建一个JavaBean:Person

public class Person {

    private String lastName;

    private Integer age;

    private Boolean boss;

    private Date birth;

    private Dog dog;


    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", dog=" + dog +
                '}';
    }
}

这个JavaBean对象中又有一个对象:Dog

public class Dog {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

接下来我们就在配置文件中进行参数的注入,但在这之前我们需要引入一个依赖:

有了这个依赖我们的配置文件才能正确的注入到JavaBean中

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
 </dependency>

本人用的是yaml语法,这样更简洁:

person:
  last-name: 小明
  age: 18
  boss: false
  birth: 2017/12/12
  dog:
   name: 小狗
   age: 2

properties版本:

person.lastName=小明
person.age=18
person.boss=false
person.birth=2017/12/12
person.dog.name=小狗
person.dog.age=2

然后我们需要在Person上加上一个注解:@ConfigurationProperties(prefix = “person”)

这个注解的意思是从配置文件中获取到以person开头的配置,注入进person类中,

效果如下,这里我为了方便进行单元测试就将Person对象添加进容器中了。

在这里插入图片描述
运行单元测试,成功注入。

在这里插入图片描述

以上…

猜你喜欢

转载自blog.csdn.net/WXZCYQ/article/details/106151967
今日推荐