YuezhenQin / javaweb

0 stars 0 forks source link

Spring Boot 3.0: Spring 6.x, JDK17, Tomcat 10.x, jakarta.servlet #2

Open YuezhenQin opened 4 months ago

YuezhenQin commented 4 months ago

Image

  1. Spring Boot 3.0 构建基于 Spring Framework 6.x 之上,需要使用 Spring Framework 6.x。
  2. 内置 tomcat, jetty (jar包部署而不是 war包部署)。
  3. 一个 application.properties 替代大量的 .xml 。
YuezhenQin commented 4 months ago

Spring

Spring is one of the most successful web application development frameworks for the Java Platform. Here is a list of some parts of Spring that provide services necessary to most real-world applications:

Spring Boot

One of the main issues of Spring is that the configuration of Spring-based applications is very complex. Spring Boot automatically configures them based on the dependencies. Spring Boot also provides a few additional features: a command-line interface, monitoring, an embedded web server (tomcat, jetty).

1. Spring Boot Starter 起步依赖

The main responsibility of Spring Boot Starter is to combine a group of related dependencies into one dependency.

如何自定义一个起步依赖 (starter)? Image 1.创建一个 dmybatis-spring-boot-autoconfigure 模块

2. Spring Boot AutoConfigurator 自动配置原理

  1. 在启动类上标记 @SpringBootApplication 注解,该注解组合了多个注解,其中包含了 @EnableAutoConfiguration。
  2. @EnableAutoConfiguration 注解又组合了 @Import 注解,该注解引导了 ImportSelector 接口的实现类 AutoConfigurationImportSelector。
  3. 该类作为接口的实现类实现了 selectImports() 方法,这个方法经过多个层次的调用,最终会读取 META-INF 目录下的,文件后缀名为 .imports 的文件。
  4. 读取到全类名之后,会解析注册条件,即 @Conditional 及其衍生注解,将符合注册条件的类的对象自动注入到 ioc 容器中。

If we use @SpringBootApplication annotation at class level, then Spring Boot AutoConfigurator will automatically add all required annotations to Java Class Bytecode.

@SpringBootApplication = @SpringBootConfiguration + @ComponentScan + @EnableAutoConfiguration.

Image

Image

引导入 AutoConfigurationImportSelector,该类是 ImportSelector 接口的实现类,重写了 selectImports() 方法,它通过多个层次的调用,最终读取了 AutoConfiguration.imports 配置文件。 Image

这个配置文件中包含了若干个全类名,完成各个类对应的对象的自动注入。

经过源码分析,我们发现 SpringBoot 的自动配置的核心在于标记一个配置类 @AutoConfiguration,将全类名书写在配置文件之中。

Image

  1. 在 .jar 包定义若干个配置类 CommonConfig。
  2. 在 .jar 包定义一个自动配置类 CommonAutoConfig(标记 @AutoConfiguration 和 @Import(CommonConfig.class))。
  3. .定义一个 META/INF/spring/common.imports 配置文件,将自动配置类的全类名配置到配置文件中
  4. 最终达到自动配置的效果。

3. Spring Boot Actuator 内置执行器

When we run our Spring Boot Web Application, Spring Boot Actuator automatically provides hostname as “localhost” and default port number as “8080”. We can access this application using “http://localhost:8080/” end point.

We actually use HTTP Request methods like GET and POST to represent Management Endpoints using Spring Boot Actuator.

YuezhenQin commented 4 months ago

@Component (@Controller 控制层, @Service 业务逻辑层, @Repository 数据访问层)

In Spring, a bean is an object of a class that is instantiated, configured, assembled and managed by Spring container. By using @Component annotation on class, Spring create its bean and add it to the application context. This bean can then be got by context.getBean(MyController.class).

To define a component, there is a special class-level annotation @Component from the org.springframework.stereotype package. Spring IoC automatically identifies all classes annotated with it and creates corresponding managed beans. By default, there is only one bean for every component.

An object of this class needs no special initialization and can be created via the default constructor. When Spring Boot starts an application, it looks for all the @Component-annotated classes and creates objects of these classes, which will then be waiting in the component container (Spring Boot 启动应用程序时,它会扫描带有@ component 注释的类,并创建这些类的对象,这些对象将在构件容器中等待).

扫描: Component Scanning and AutoConfiguration

In SpringBoot, we use @SpringBootApplication to enable both component scanning and autoconfiguration.

@SpringBootApplication = @Configuration + @ComponentScan(basePackages="com.xxx") + @EnableAutoConfigiration

注册: 将不同来源的 Bean 注册到 IoC 容器中

  1. 将标记了 @Component (@Controller, @Service, @Repository) 的类的 Bean 对象注册到容器中 controller: @RestController, @RequestMapping("/"), @Autowired: service service: @Service, @Autowired: mapper repository: @Repository; @Mapper (Mybatis only)

  2. 将非自定义的、第三方类的 Bean 对象注册到容器中 2.1 @Bean (存), applicationcontext.getBean(XXX.class) (取) 2.2 @Import

设置注册条件: @Conditional

@ConditionalOnProperty(prefix="", name = {"", ""}) @ConditionalOnMissingBean(XXX.class) @ConditionalOnClass(name = "org.xxx.xxx")

YuezhenQin commented 3 months ago

Image Image

[1] Annotations in Spring Boot, https://blog.stackademic.com/choosing-the-right-annotation-restcontroller-vs-controller-in-spring-boot-49ad4a95160b

YuezhenQin commented 1 month ago

SpringBoot 第一个工程创建

Image

  1. Spring Initializer 快速创建工程 Image

  2. 引入起步依赖的坐标 Image

  3. 管理起步依赖的版本 Image

  4. 创建一个 controller Image

Image

SpringBoot 工程结构解析

YuezhenQin commented 1 month ago

1. spring boot 的配置文件: application.properties, application.yml, application.yaml

Image

Image

reference: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html

.yml 配置信息的取得

@Value(${prefix}), @ConfigurationProperties(prefix="prefix")

YuezhenQin commented 1 month ago

.

YuezhenQin commented 1 month ago

The release of Spring Boot 3.3.0 delivers dependency upgrades and new features such as: improved startup times and reduced memory consumption by adding support for Class Data Sharing (CDS); virtual thread support for web sockets; and security improvements, for example, auto-configuration for the Spring Security ... class. More details on this release may be found in the release notes.

YuezhenQin commented 2 weeks ago

Spring 注解

@Component

@Configuration, @Bean

@Import()

@EnableXXX 封装 @Import()

Image

@Target({ElementType.TYPE}) //该注解可以在类上标记
@Retention(RetentionPolicy.RUNTIME) //该注解在运行时阶段持续运行
@Import(CommonImportSelector.class)
public @interface EnableCommonConfig {
}

@Value("${}"): 取得配置文件中键 K 对应的值 V

@Autowired, inject the bean automatically

Spring Boot 注解

YuezhenQin commented 1 week ago

Dependency Injection

In OOP, dependencies appear in the form of instance creation inside a class (在OOP中,“依赖关系”以在一个类中创建另一个类的实例的形式出现). Through this instance, one class can access fields and methods of another class. This way classses become dependent.

In terms of Maven or Gradle, the external libraries or frameworks used in a project are called dependencies, which are packaged in jar (java archive) files.

Dependency injection or DI is the process of transfer the task of object creation to other parts of code. The objects define their dependencies (the other objects they interact with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The IoC container then injects those dependencies when it creates the bean.

There are three types of DI, in which constructor injection is the most recommended and preferable option for mandatory dependencies.

@Configuration
public class AppConfig {

    @Bean
    public BeanOne beanOne() {
        return new BeanOne(beanTwo());
    }

    @Bean
    public BeanTwo beanTwo() {
        return new BeanTwo();
    }
}

In the preceding example, beanOne receives a reference to beanTwo through constructor injection.

In the Spring framework, @Autowired and @Resource are annotations used for dependency injection, which is a way to achieve Inversion of Control (IoC) by allowing the Spring container to inject dependencies into components. @Autowired is Spring's annotation, while @Resource is specified by the JSR-250 Java standard.