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
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
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> |