Open YuezhenQin opened 4 months ago
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:
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).
The main responsibility of Spring Boot Starter is to combine a group of related dependencies into one dependency.
如何自定义一个起步依赖 (starter)? 1.创建一个 dmybatis-spring-boot-autoconfigure 模块
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.
引导入 AutoConfigurationImportSelector,该类是 ImportSelector 接口的实现类,重写了 selectImports() 方法,它通过多个层次的调用,最终读取了 AutoConfiguration.imports 配置文件。
这个配置文件中包含了若干个全类名,完成各个类对应的对象的自动注入。
经过源码分析,我们发现 SpringBoot 的自动配置的核心在于标记一个配置类 @AutoConfiguration,将全类名书写在配置文件之中。
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.
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 注释的类,并创建这些类的对象,这些对象将在构件容器中等待).
In SpringBoot, we use @SpringBootApplication to enable both component scanning and autoconfiguration.
@SpringBootApplication = @Configuration + @ComponentScan(basePackages="com.xxx") + @EnableAutoConfigiration
将标记了 @Component (@Controller, @Service, @Repository) 的类的 Bean 对象注册到容器中 controller: @RestController, @RequestMapping("/"), @Autowired: service service: @Service, @Autowired: mapper repository: @Repository; @Mapper (Mybatis only)
将非自定义的、第三方类的 Bean 对象注册到容器中 2.1 @Bean (存), applicationcontext.getBean(XXX.class) (取) 2.2 @Import
@ConditionalOnProperty(prefix="", name = {"", ""}) @ConditionalOnMissingBean(XXX.class) @ConditionalOnClass(name = "org.xxx.xxx")
[1] Annotations in Spring Boot, https://blog.stackademic.com/choosing-the-right-annotation-restcontroller-vs-controller-in-spring-boot-49ad4a95160b
Spring Initializer 快速创建工程
引入起步依赖的坐标
管理起步依赖的版本
创建一个 controller
reference: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html
@Value(${prefix}), @ConfigurationProperties(prefix="prefix")
.
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.
@Target({ElementType.TYPE}) //该注解可以在类上标记
@Retention(RetentionPolicy.RUNTIME) //该注解在运行时阶段持续运行
@Import(CommonImportSelector.class)
public @interface EnableCommonConfig {
}
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.