Skip to content

Commit 029168a

Browse files
committed
Update Java Notes
1 parent 8fefef0 commit 029168a

1 file changed

Lines changed: 49 additions & 32 deletions

File tree

SSM.md

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6342,22 +6342,22 @@ public class UserServiceJDKProxy {
63426342

63436343
#### CGLIB
63446344

6345-
CGLIB(Code Generation Library):Code生成类库
6345+
CGLIB(Code Generation Library):Code 生成类库
63466346

63476347
CGLIB 特点:
63486348

63496349
* CGLIB 动态代理**不限定**是否具有接口,可以对任意操作进行增强
63506350
* CGLIB 动态代理无需要原始被代理对象,动态创建出新的代理对象
6351-
* CGLIB **继承被代理类**,如果代理类是final则不能实现
6351+
* CGLIB **继承被代理类**,如果代理类是 final 则不能实现
63526352

63536353
![](https://gitee.com/seazean/images/raw/master/Frame/AOP底层原理-cglib.png)
63546354

6355-
* cglib类
6355+
* CGLIB 类
63566356

6357-
* JDKProxy仅对接口方法做增强,cglib对所有方法做增强,包括Object类中的方法 (toString、hashCode)
6357+
* JDKProxy 仅对接口方法做增强,CGLIB 对所有方法做增强,包括 Object 类中的方法(toString、hashCode
63586358
* 返回值类型采用多态向下转型,所以需要设置父类类型
63596359

6360-
需要对方法进行判断是否是save,来选择性增强
6360+
需要对方法进行判断是否是 save,来选择性增强
63616361

63626362
```java
63636363
public class UserServiceImplCglibProxy {
@@ -14207,7 +14207,7 @@ public class MyConfig {
1420714207
```
1420814208

1420914209
```xml
14210-
<beans ...">
14210+
<beans ...>
1421114211
<bean id="haha" class="com.lun.boot.bean.User">
1421214212
<property name="name" value="zhangsan"></property>
1421314213
<property name="age" value="18"></property>
@@ -14346,6 +14346,10 @@ SpringApplication#run(String... args):
1434614346

1434714347
* `refreshContext(context)`:**刷新 IOC 容器**
1434814348

14349+
* Spring 的容器启动流程
14350+
* `invokeBeanFactoryPostProcessors(beanFactory)`:**实现了自动装配**
14351+
* `onRefresh()`:**创建 WebServer** 使用该接口
14352+
1434914353
* `afterRefresh(context, applicationArguments)`:留给用户自定义容器刷新完成后的处理逻辑
1435014354

1435114355
* `stopWatch.stop()`:记录应用启动完成的时间
@@ -14418,7 +14422,7 @@ SpringBoot 定义了一套接口规范,这套规范规定 SpringBoot 在启动
1441814422
}
1441914423
````
1442014424

14421-
* @AutoConfigurationPackage:**将添加该注解的类所在的 package 作为自动配置 package 进行管理**,把启动类所在的包设置一次,为了给各种自动配置的第三方库扫描用,比如带 @Mapper 注解的类,Spring 自身其实是不认识的,但自动配置的 Mybatis 需要扫描用到,而 ComponentScan 用来扫描注解类,并没有提供接口给三方使。
14425+
* @AutoConfigurationPackage:**将添加该注解的类所在的 package 作为自动配置 package 进行管理**,把启动类所在的包设置一次,为了给各种自动配置的第三方库扫描用,比如带 @Mapper 注解的类,Spring 自身是不能识别的,但自动配置的 Mybatis 需要扫描用到,而 ComponentScan 只是用来扫描注解类,并没有提供接口给三方使用
1442214426

1442314427
```java
1442414428
@Import(AutoConfigurationPackages.Registrar.class) // 利用 Registrar 给容器中导入组件
@@ -14431,43 +14435,56 @@ SpringBoot 定义了一套接口规范,这套规范规定 SpringBoot 在启动
1443114435
`register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]))`:注册 BD
1443214436

1443314437
* `new PackageImports(metadata).getPackageNames()`:获取添加当前注解的类的所在包
14434-
* `registry.registerBeanDefinition(BEAN, new BasePackagesBeanDefinition(packageNames))`:
14438+
* `registry.registerBeanDefinition(BEAN, new BasePackagesBeanDefinition(packageNames))`:存放到容器中
14439+
* `new BasePackagesBeanDefinition(packageNames)`:把当前主类所在的包名封装到该对象中
1443514440

1443614441
* @Import(AutoConfigurationImportSelector.class):**首先自动装配的核心类**
1443714442

14443+
容器刷新时执行:**invokeBeanFactoryPostProcessors()** → invokeBeanDefinitionRegistryPostProcessors() → postProcessBeanDefinitionRegistry() → processConfigBeanDefinitions() → parse() → process() → processGroupImports() → getImports() → process() → **AutoConfigurationImportSelector#getAutoConfigurationEntry()**
14444+
1443814445
```java
14439-
// 选择导入的类
14440-
public String[] selectImports(AnnotationMetadata annotationMetadata) {
14441-
//判断自动装配开关是否打开
14446+
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
1444214447
if (!isEnabled(annotationMetadata)) {
14443-
return NO_IMPORTS;
14444-
}
14445-
//获取需要自动装配的配置类
14446-
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
14447-
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
14448+
return EMPTY_ENTRY;
14449+
}
14450+
// 获取注解属性,@SpringBootApplication 注解的 exclude 属性和 excludeName 属性
14451+
AnnotationAttributes attributes = getAttributes(annotationMetadata);
14452+
// 获取所有需要自动装配的候选项
14453+
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
14454+
// 去除重复的选项
14455+
configurations = removeDuplicates(configurations);
14456+
// 获取注解配置的排除的自动装配类
14457+
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
14458+
checkExcludedClasses(configurations, exclusions);
14459+
// 移除所有的配置的不需要自动装配的类
14460+
configurations.removeAll(exclusions);
14461+
// 过滤,条件装配
14462+
configurations = getConfigurationClassFilter().filter(configurations);
14463+
// 获取 AutoConfigurationImportListener 类的监听器调用 onAutoConfigurationImportEvent 方法
14464+
fireAutoConfigurationImportEvents(configurations, exclusions);
14465+
// 包装成 AutoConfigurationEntry 返回
14466+
return new AutoConfigurationEntry(configurations, exclusions);
1444814467
}
1444914468
```
1445014469

14451-
`getAutoConfigurationEntry(annotationMetadata)`:
14452-
14453-
* `attributes = getAttributes(annotationMetadata)`:获取注解的属性信息
14454-
14455-
* `getCandidateConfigurations(annotationMetadata, attributes)`:**获取自动配置的候选项**
14470+
AutoConfigurationImportSelector#getCandidateConfigurations:获取自动配置的候选项
1445614471

14457-
* `List<String> configurations = SpringFactoriesLoader.loadFactoryNames()`:加载资源
14472+
* `List<String> configurations = SpringFactoriesLoader.loadFactoryNames()`:加载自动配置类
1445814473

14459-
参数一:`getSpringFactoriesLoaderFactoryClass()` 获取 @EnableAutoConfiguration 注解类
14474+
参数一:`getSpringFactoriesLoaderFactoryClass()` 获取 @EnableAutoConfiguration 注解类
1446014475

14461-
参数二:`getBeanClassLoader()` 获取类加载器
14476+
参数二:`getBeanClassLoader()` 获取类加载器
1446214477

14478+
* `factoryTypeName = factoryType.getName()`:@EnableAutoConfiguration 注解的全类名
14479+
* `return loadSpringFactories(classLoaderToUse).getOrDefault()`:加载资源
1446314480
* `urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION)`:获取资源类
14464-
* `FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"`:获取位置
14481+
* `FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"`:**加载的资源的位置**
1446514482

14466-
* 从 spring-boot-autoconfigure-2.5.3.jar/META-INF/spring.factories 文件中获取自动装配类,**进行条件装配,按需装配**
14483+
* `return configurations`:返回所有自动装配类的候选项
1446714484

14468-
* `return new AutoConfigurationEntry(configurations, exclusions)`:封装返回
14485+
* 从 spring-boot-autoconfigure-2.5.3.jar/META-INF/spring.factories 文件中获取自动装配类,**进行条件装配,按需装配**
1446914486

14470-
![](https://gitee.com/seazean/images/raw/master/Frame/SpringBoot-自动装配配置文件.png)
14487+
![](https://gitee.com/seazean/images/raw/master/Frame/SpringBoot-自动装配配置文件.png)
1447114488

1447214489

1447314490

@@ -15247,7 +15264,7 @@ SpringBoot 嵌入式 Servlet 容器,默认支持的 webServe:Tomcat、Jetty
1524715264
<groupId>org.springframework.boot</groupId>
1524815265
<artifactId>spring-boot-starter-web</artifactId>
1524915266
<exclusions>
15250-
<exclusion><!--必须要把内嵌的 Tomcat 容器-->
15267+
<exclusion> <!--必须要把内嵌的 Tomcat 容器-->
1525115268
<groupId>org.springframework.boot</groupId>
1525215269
<artifactId>spring-boot-starter-tomcat</artifactId>
1525315270
</exclusion>
@@ -15259,13 +15276,13 @@ SpringBoot 嵌入式 Servlet 容器,默认支持的 webServe:Tomcat、Jetty
1525915276
</dependency>
1526015277
```
1526115278

15262-
源码分析 ServletWebServerFactoryAutoConfiguration
15279+
创建 Web 容器
1526315280

1526415281
* `SpringApplication.run(BootApplication.class, args)`:应用启动
1526515282

1526615283
* `ConfigurableApplicationContext.run()`:
1526715284

15268-
* `context = createApplicationContext()`:创建容器
15285+
* `context = createApplicationContext()`:**创建容器**
1526915286

1527015287
* `applicationContextFactory = ApplicationContextFactory.DEFAULT`
1527115288

@@ -15315,7 +15332,7 @@ SpringBoot 嵌入式 Servlet 容器,默认支持的 webServe:Tomcat、Jetty
1531515332

1531615333
`TomcatServletWebServerFactory`、`JettyServletWebServerFactory`、`UndertowServletWebServerFactory`
1531715334

15318-
- 自动配置类 ServletWebServerFactoryAutoConfiguration 导入了 ServletWebServerFactoryConfiguration(配置类),根据条件装配判断系统中到底导入了哪个 Web 服务器的包,创建出服务器并启动
15335+
- **自动配置类 ServletWebServerFactoryAutoConfiguration** 导入了 ServletWebServerFactoryConfiguration(配置类),根据条件装配判断系统中到底导入了哪个 Web 服务器的包,创建出服务器并启动
1531915336

1532015337
- 默认是 web-starter 导入 tomcat 包,容器中就有 TomcatServletWebServerFactory,创建出 Tomcat 服务器并启动,
1532115338

0 commit comments

Comments
 (0)