-
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
14 changed files
with
136 additions
and
0 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 |
---|---|---|
@@ -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
10
src/main/java/springframework/config/SingletonBeanRegistry.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,10 @@ | ||
package springframework.config; | ||
|
||
/** | ||
* @Author:吉口吉 | ||
* @Date:Created in 18:16 2021/12/5 | ||
* @Description: | ||
*/ | ||
public interface SingletonBeanRegistry {//缓存,单例注册表 | ||
public Object getSingleton(String beanName); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/springframework/support/AbstractAutowireCapableBeanFactory.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 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
22
src/main/java/springframework/support/AbstractBeanFactory.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 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
12
src/main/java/springframework/support/BeanDefinitionRegistry.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,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); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/springframework/support/DefaultListableBeanFactory.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 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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/springframework/support/DefaultSingletonBeanRegistry.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,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 added
BIN
+1.39 KB
target/classes/springframework/support/AbstractAutowireCapableBeanFactory.class
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+245 Bytes
target/classes/springframework/support/BeanDefinitionRegistry.class
Binary file not shown.
Binary file added
BIN
+1.63 KB
target/classes/springframework/support/DefaultListableBeanFactory.class
Binary file not shown.
Binary file added
BIN
+1.05 KB
target/classes/springframework/support/DefaultSingletonBeanRegistry.class
Binary file not shown.