forked from DerekYRC/mini-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
267 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# <img src="assets/spring-framework.png" width="80" height="80"> mini-spring | ||
[![Build Status](https://img.shields.io/badge/build-passing-brightgreen)](https://github.com/DerekYRC/mini-spring) | ||
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) | ||
[![Stars](https://img.shields.io/github/stars/DerekYRC/mini-spring)](https://img.shields.io/github/stars/DerekYRC/mini-spring) | ||
[![Forks](https://img.shields.io/github/forks/DerekYRC/mini-spring)](https://img.shields.io/github/forks/DerekYRC/mini-spring) | ||
|
||
## About | ||
* [中文版](./README_CN.md) | ||
|
@@ -38,16 +40,22 @@ If this project can help you, please give a **STAR, thank you!!!** | |
|
||
#### Expanding | ||
* [PropertyPlaceholderConfigurer](#PropertyPlaceholderConfigurer) | ||
* [Type conversion](#类型转换) | ||
* [Package scan](#包扫描) | ||
* [Autowired annotation](#基于注解的依赖注入Autowired) | ||
* [@Autowired and @Value annotation](#基于注解@Autowired和@Value的依赖注入) | ||
* [Type conversion](#类型转换) | ||
|
||
#### Advanced | ||
* [Solve the problem of circular dependencies](#解决循环依赖问题) | ||
|
||
## Usage | ||
Each function point corresponds to a branch. Switch to the branch corresponding to the function point to see the new function. The incremental change point is described in the [changelog.md](https://github.com/DerekYRC/mini-spring/blob/main/changelog.md) file. | ||
|
||
## Contributing | ||
Any contributions you make are greatly appreciated. | ||
|
||
## Contact | ||
Please feel free to ask me any questions related to mini-spring and other technologies. My email is **[email protected]**. | ||
|
||
## Reference | ||
- [《Spring源码深度解析》](https://book.douban.com/subject/25866350/) | ||
- [《精通Spring 4.x》](https://book.douban.com/subject/26952826/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# <img src="assets/spring-framework.png" width="80" height="80"> mini-spring | ||
[![Build Status](https://img.shields.io/badge/build-passing-brightgreen)](https://github.com/DerekYRC/mini-spring) | ||
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) | ||
[![Stars](https://img.shields.io/github/stars/DerekYRC/mini-spring)](https://img.shields.io/github/stars/DerekYRC/mini-spring) | ||
[![Forks](https://img.shields.io/github/forks/DerekYRC/mini-spring)](https://img.shields.io/github/forks/DerekYRC/mini-spring) | ||
|
||
## 关于 | ||
* [English version](./README.md) | ||
|
@@ -38,16 +40,23 @@ | |
|
||
#### 扩展篇 | ||
* [PropertyPlaceholderConfigurer](#PropertyPlaceholderConfigurer) | ||
* [类型转换](#类型转换) | ||
* [包扫描](#包扫描) | ||
* [基于注解的依赖注入Autowired](#基于注解的依赖注入Autowired) | ||
* [基于注解@Autowired和@Value的依赖注入](#基于注解@Autowired和@Value的依赖注入) | ||
* [类型转换](#类型转换) | ||
|
||
#### 高级篇 | ||
* [解决循环依赖问题](#解决循环依赖问题) | ||
|
||
## 使用方法 | ||
每个功能点对应一个分支,切换到功能点对应的分支了解新增的功能,增量改动点在[changelog.md](https://github.com/DerekYRC/mini-spring/blob/main/changelog.md)文件中描述。 | ||
|
||
## 贡献 | ||
欢迎Pull Request | ||
|
||
## 联系我 | ||
欢迎探讨跟mini-spring和其他技术相关的问题,个人邮箱:**[email protected]** | ||
|
||
|
||
## 参考 | ||
- [《Spring源码深度解析》](https://book.douban.com/subject/25866350/) | ||
- [《精通Spring 4.x》](https://book.douban.com/subject/26952826/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.springframework.context.annotation; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/26 | ||
*/ | ||
public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateComponentProvider { | ||
|
||
private BeanDefinitionRegistry registry; | ||
|
||
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
public void doScan(String... basePackages) { | ||
for (String basePackage : basePackages) { | ||
Set<BeanDefinition> candidates = findCandidateComponents(basePackage); | ||
for (BeanDefinition candidate : candidates) { | ||
// 解析bean的作用域 | ||
String beanScope = resolveBeanScope(candidate); | ||
if (StrUtil.isNotEmpty(beanScope)) { | ||
candidate.setScope(beanScope); | ||
} | ||
//生成bean的名称 | ||
String beanName = determineBeanName(candidate); | ||
//注册BeanDefinition | ||
registry.registerBeanDefinition(beanName, candidate); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 获取bean的作用域 | ||
* | ||
* @param beanDefinition | ||
* @return | ||
*/ | ||
private String resolveBeanScope(BeanDefinition beanDefinition) { | ||
Class<?> beanClass = beanDefinition.getBeanClass(); | ||
Scope scope = beanClass.getAnnotation(Scope.class); | ||
if (scope != null) { | ||
return scope.value(); | ||
} | ||
|
||
return StrUtil.EMPTY; | ||
} | ||
|
||
|
||
/** | ||
* 生成bean的名称 | ||
* | ||
* @param beanDefinition | ||
* @return | ||
*/ | ||
private String determineBeanName(BeanDefinition beanDefinition) { | ||
Class<?> beanClass = beanDefinition.getBeanClass(); | ||
Component component = beanClass.getAnnotation(Component.class); | ||
String value = component.value(); | ||
if (StrUtil.isEmpty(value)) { | ||
value = StrUtil.lowerFirst(beanClass.getSimpleName()); | ||
} | ||
return value; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...a/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.springframework.context.annotation; | ||
|
||
import cn.hutool.core.util.ClassUtil; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/26 | ||
*/ | ||
public class ClassPathScanningCandidateComponentProvider { | ||
|
||
public Set<BeanDefinition> findCandidateComponents(String basePackage) { | ||
Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>(); | ||
// 扫描有org.springframework.stereotype.Component注解的类 | ||
Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Component.class); | ||
for (Class<?> clazz : classes) { | ||
BeanDefinition beanDefinition = new BeanDefinition(clazz); | ||
candidates.add(beanDefinition); | ||
} | ||
return candidates; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/springframework/context/annotation/Scope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.springframework.context.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/26 | ||
*/ | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface Scope { | ||
|
||
String value() default "singleton"; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/springframework/stereotype/Component.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.springframework.stereotype; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/26 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface Component { | ||
|
||
String value() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/java/org/springframework/test/ioc/PackageScanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.springframework.test.ioc; | ||
|
||
import org.junit.Test; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
import org.springframework.test.bean.Car; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/26 | ||
*/ | ||
public class PackageScanTest { | ||
|
||
@Test | ||
public void testScanPackage() throws Exception { | ||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:package-scan.xml"); | ||
|
||
Car car = applicationContext.getBean("car", Car.class); | ||
assertThat(car).isNotNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context | ||
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> | ||
|
||
<context:component-scan base-package="org.springframework.test.bean"/> | ||
|
||
</beans> |