废话不多少直接上代码
spring 常用注解先定义一下
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Autowired {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComponentScan {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Scope {
String value() default "";
}
bean 定义
public class BeanDefinition {
private Class type;
private String scope;
private boolean isLazy;
public Class getType() {
return type;
}
public void setType(Class type) {
this.type = type;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public boolean isLazy() {
return isLazy;
}
public void setLazy(boolean lazy) {
isLazy = lazy;
}
}
public interface BeanPostProcessor {
default Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
default Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
}
此处是重点
public class DjyApplicationContext {
private Class configClass;
private Map<String, BeanDefinition> beanDefinitionMap = new HashMap();
private Map<String, Object> singletonObjects = new HashMap();
private List<BeanPostProcessor> beanPostProcessorList = new ArrayList<>();
/**
* 初始化
*
* @param configClass
*/
public DjyApplicationContext(Class configClass) {
this.configClass = configClass;
//扫描
scan(configClass);
for (Map.Entry<String, BeanDefinition> entry : beanDefinitionMap.entrySet()) {
String beanNme = entry.getKey();
BeanDefinition beanDefinition = entry.getValue();
if (beanDefinition.getScope().equals("singleton")) {
Object bean = createBean(beanNme, beanDefinition);
singletonObjects.put(beanNme, bean);
}
}
}
/**
* 获得bean
*
* @param beanName
* @return
*/
public Object getBean(String beanName) {
//不存在表示Bean不存在
if (!beanDefinitionMap.containsKey(beanName)) {
throw new NullPointerException("Bean不存在");
}
//根据bean的名称获取 bean的实体clazz
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition.getScope().equals("singleton")) {
Object singleton = singletonObjects.get(beanName);
if (null == singleton) {
singleton = createBean(beanName, beanDefinition);
singletonObjects.put(beanName, beanDefinition);
}
return singleton;
} else {
//原型bean ==>多例
Object prototypeBean = createBean(beanName, beanDefinition);
return prototypeBean;
}
}
/**
* 创建bean
*
* @param beanName
* @param beanDefinition
* @return
*/
private Object createBean(String beanName, BeanDefinition beanDefinition) {
Class clazz = beanDefinition.getType();
Object instance = null;
try {
instance = clazz.getConstructor().newInstance();
//此处进行依赖注入(AOP)
for (Field field : clazz.getDeclaredFields()) {
if (field.isAnnotationPresent(Autowired.class)) {
field.setAccessible(true);
//此处先根据类型查找 在根据名字查找
//此处省略 直接名字查找 有兴趣的小伙伴可自行尝试
field.set(instance, getBean(field.getName()));
}
}
if (instance instanceof InitializingBean) {
((InitializingBean) instance).afterPropertiesSet();
}
new DjyBeanPostProcessor().postProcessAfterInitialization(instance, beanName);
for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
beanPostProcessor.postProcessAfterInitialization(instance, beanName);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return instance;
}
/**
* 扫描所有Bean
*
* @param configClass
*/
private void scan(Class configClass) {
if (configClass.isAnnotationPresent(ComponentScan.class)) {
ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
String path = componentScanAnnotation.value();
path = path.replace(".", "/");
ClassLoader classLoader = DjyApplicationContext.class.getClassLoader();
URL resource = classLoader.getResource(path);
File file = new File(resource.getFile());
if (file.isDirectory()) {
for (File f : file.listFiles()) {
String absolutePath = f.getAbsolutePath();
absolutePath = absolutePath.substring(absolutePath.indexOf("com"), absolutePath.indexOf(".class"));
absolutePath = absolutePath.replace("\\", ".");
Class<?> clazz = null;
try {
clazz = classLoader.loadClass(absolutePath);
if (clazz.isAnnotationPresent(Component.class)) {
//判端当前类是否是实现BeanPostProcessor这个接口
if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
BeanPostProcessor instance = (BeanPostProcessor) clazz.getConstructor().newInstance();
beanPostProcessorList.add(instance);
} else {
// 通过注解获取当前bean的名字 spring 不定义会默认生成
Component componentAnnotation = clazz.getAnnotation(Component.class);
String beanName = componentAnnotation.value();
if ("".equals(beanName)) {
beanName = Introspector.decapitalize(clazz.getSimpleName());
}
//如果存在Component ==>说明是bean 开始构建bean的定义
BeanDefinition beanDefinition = new BeanDefinition();
beanDefinition.setType(clazz);
if (clazz.isAnnotationPresent(Scope.class)) {
Scope scopeAnnotation = clazz.getAnnotation(Scope.class);
String value = scopeAnnotation.value();
beanDefinition.setScope(value);
} else {
//单例
beanDefinition.setScope("singleton");
}
beanDefinitionMap.put(beanName, beanDefinition);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
}
}
}
public interface InitializingBean {
void afterPropertiesSet();
}
以下是业务测试代码
@Component
public class DjyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
System.out.println("1111");
return bean;
}
}
@Component
public class OrderService {
public void test() {
System.out.println("test");
}
}
@Component("userService")
@Scope("prototype")
public class UserService implements InitializingBean {
@Autowired
private OrderService orderService;
public void test() {
System.out.println(orderService);
System.out.println("test");
}
@Override
public void afterPropertiesSet() {
System.out.println("test1111");
}
}
@ComponentScan("com.djy.service")
public class AppConfig {
}
以上是博主本人闲暇之余学习所记录,仅供大家学习参考