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
16 changed files
with
281 additions
and
16 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
61 changes: 61 additions & 0 deletions
61
...va/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.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,61 @@ | ||
package org.springframework.beans.factory.annotation; | ||
|
||
import cn.hutool.core.bean.BeanUtil; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.PropertyValues; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.beans.factory.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* 处理@Autowired和@Value注解的BeanPostProcessor | ||
* | ||
* @author derekyi | ||
* @date 2020/12/27 | ||
*/ | ||
public class AutowiredAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor, BeanFactoryAware { | ||
|
||
private ConfigurableListableBeanFactory beanFactory; | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; | ||
} | ||
|
||
@Override | ||
public PropertyValues postProcessPropertyValues(PropertyValues pvs, Object bean, String beanName) throws BeansException { | ||
//处理@Value注解 | ||
Class<?> clazz = bean.getClass(); | ||
Field[] fields = clazz.getDeclaredFields(); | ||
for (Field field : fields) { | ||
Value valueAnnotation = field.getAnnotation(Value.class); | ||
if (valueAnnotation != null) { | ||
String value = valueAnnotation.value(); | ||
value = beanFactory.resolveEmbeddedValue(value); | ||
BeanUtil.setFieldValue(bean, field.getName(), value); | ||
} | ||
} | ||
|
||
//处理@Autowired注解(下一节实现) | ||
|
||
return pvs; | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
return null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/springframework/beans/factory/annotation/Value.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,17 @@ | ||
package org.springframework.beans.factory.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author derekyi | ||
* @date 2020/12/27 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) | ||
public @interface Value { | ||
|
||
String value(); | ||
} |
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
Oops, something went wrong.