Skip to content

Commit

Permalink
0.2ver
Browse files Browse the repository at this point in the history
  • Loading branch information
s-zh committed Dec 5, 2021
1 parent 7900565 commit 83ec29f
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/springframework/config/BeanDefinition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package springframework.config;

/**
* @Author:吉口吉
* @Date:Created in 18:10 2021/12/5
* @Description:
*/
public class BeanDefinition {//类信息,封装Class
private Class beanClass;

public BeanDefinition(Class beanClass){
this.beanClass=beanClass;
}
public Class getBeanClass(){
return this.beanClass;
}
public void setBeanClass(Class beanClass){
this.beanClass=beanClass;
}
}
10 changes: 10 additions & 0 deletions src/main/java/springframework/config/SingletonBeanRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package springframework.config;

/**
* @Author:吉口吉
* @Date:Created in 18:16 2021/12/5
* @Description:
*/
public interface SingletonBeanRegistry {//缓存,单例注册表
public Object getSingleton(String beanName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package springframework.support;

import springframework.config.BeanDefinition;

/**
* @Author:吉口吉
* @Date:Created in 18:51 2021/12/5
* @Description:
*/
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
@Override
protected Object createBean(String beanName,BeanDefinition beanDefinition) {
Object bean=null;
try {
bean=beanDefinition.getBeanClass().newInstance();//通过反射创建
} catch (InstantiationException | IllegalAccessException e) {
System.err.println("实例化Bean失败");
}
addSingleton(beanName,bean);//加入单例注册表中
return bean;
}
}
22 changes: 22 additions & 0 deletions src/main/java/springframework/support/AbstractBeanFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package springframework.support;

import springframework.BeanFactory;
import springframework.config.BeanDefinition;

/**
* @Author:吉口吉
* @Date:Created in 18:44 2021/12/5
* @Description:
*/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name){
Object bean =getSingleton(name);
if(bean!=null) return bean;
BeanDefinition beanDefinition=getBeanDefinition(name);//使用抽象方法
return createBean(name,beanDefinition);//使用抽象方法
}

protected abstract BeanDefinition getBeanDefinition(String beanName);
protected abstract Object createBean(String beanName,BeanDefinition beanDefinition);
}
12 changes: 12 additions & 0 deletions src/main/java/springframework/support/BeanDefinitionRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package springframework.support;

import springframework.config.BeanDefinition;

/**
* @Author:吉口吉
* @Date:Created in 18:14 2021/12/5
* @Description:
*/
public interface BeanDefinitionRegistry {//类信息注册表
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package springframework.support;

import springframework.config.BeanDefinition;

import java.util.HashMap;

/**
* @Author:吉口吉
* @Date:Created in 18:58 2021/12/5
* @Description:
*/
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry {
private HashMap<String,BeanDefinition> beanDefinitionHashMap=new HashMap<>();

@Override
protected BeanDefinition getBeanDefinition(String beanName) {
BeanDefinition beanDefinition=beanDefinitionHashMap.get(beanName);
if(beanDefinition==null) System.err.println("beanName为"+beanName+"的bean没有被定义");
return beanDefinition;
}

@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionHashMap.put(beanName,beanDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package springframework.support;

import springframework.config.SingletonBeanRegistry;

import java.util.HashMap;

/**
* @Author:吉口吉
* @Date:Created in 18:18 2021/12/5
* @Description:
*/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private HashMap<String,Object> singletonObjects=new HashMap<>();

@Override
public Object getSingleton(String beanName){
return singletonObjects.get(beanName);
}

protected void addSingleton(String beanName,Object singletonObject){
singletonObjects.put(beanName,singletonObject);
}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 83ec29f

Please sign in to comment.