1.循环问题(使用一级缓存和实例化后就加入到一级缓存来解决)
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
private void refresh()throws Exception{
// 解析配置,注册BeanDefinition
loadBeanDefinitions();
// 创建所有单例Bean
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
// 创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
// 创建所有的bean
private void finishBeanFactoryInitialization()throws Exception{
//
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
// 如果没有就开始创建
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
return bean;
}
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
}
// 测试
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
InstanceA instanceA = (InstanceA)context.getBean("instanceA");
instanceA.say();
上面代码有循环依赖问题,解决需要修改如下代码
// 1.实例化 反射
...
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);
//解决循环依赖就是使用一级缓存,在bean实例化后就加入到一级缓存中去
2.遇到多线程获取bean不完整问题(一级缓存加锁来解决)
问题:
getBean的时候,线程一先getBean进去拿beanA,存进一级缓存,线程二也进去getBean拿A,直接拿到一级缓存中的A,但是A还没属性注入,是不完整的,问题就在这里
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
private void refresh()throws Exception{
// 解析配置,注册BeanDefinition
loadBeanDefinitions();
// 创建所有单例Bean
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
// 创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
// 创建所有的bean
private void finishBeanFactoryInitialization()throws Exception{
//
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
// 如果没有就开始创建
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);///这里在bean实例化后就加入一级缓存,可以解决循环依赖问题
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
return bean;
}
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
}
// 测试
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
InstanceA instanceA = (InstanceA)context.getBean("instanceA");
instanceA.say();
多线程getBean获取不完整bean问题解决===》将一级缓存获取bean锁起来:
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 这里加锁,解决多线程bean不完整问题
synchronized(singletonObjects){
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
//...此处省略
return bean;
}
}
3.遇到多线程获取bean有锁的性能问题(使用二级缓存,且不锁一级缓存,换到只锁二级缓存)
问题:
如beanc已经创建好了,在线程一第一次getbean beana的时候,线程二进来getbean beanc的时候,因为被锁住了所以要等待线程一getbean beana完后才线程二才可以getbean beanc,这个影响性能,所以需要使用二级缓存
问题关键是不应该把getBean里面从一级缓存拿bean的也给锁住,这样别的线程进来取bean就会卡住等待,影响性能,因此需要另外一个缓存出现(二级缓存)
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
private void refresh()throws Exception{
// 解析配置,注册BeanDefinition
loadBeanDefinitions();
// 创建所有单例Bean
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
// 创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
// 创建所有的bean
private void finishBeanFactoryInitialization()throws Exception{
//
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 这里加锁,解决多线程bean不完整问题
synchronized(singletonObjects){
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
// 如果没有就开始创建
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);///这里在bean实例化后就加入一级缓存,可以解决循环依赖问题
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
return bean;
}
}
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
}
问题解决: 1.新建一个二级缓存 2.一级缓存不锁进去,只锁二级缓存 3.最终一级缓存存了完整bean后删除二级缓存中的同名bean
这样的话可以解决该问题
新增二级缓存
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
private final Map<String,Object> earlySingletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
只锁二级缓存,不锁一级缓存
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
// 如果没有就开始创建
/// ***********修改的地方**************从上面移到下面这里只锁二级缓存
synchronized(singletonObjects){
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
/// ***********修改的地方**************
earlySingletonObjects .put(beanName,bean);///这里直接换成二级缓存来存实例化后的bean
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
//***********修改的地方**************
earlySingletonObjects.remove(beanName);
return bean;
}
}
锁二级缓存
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
synchronized(earlySingletonObjects){
if(earlySingletonObjects.containsKey(beanName)){
return earlySingletonObjects.get(beanName);
}
}
}
备注:
4.解决并发还有一个问题
问题:
A依赖B,B依赖A
1.线程一先进来getBean A 先从一级缓存去 没有进二级缓存中取没有,实例化后A存入二级缓存
2.此时线程二进来也getBean A,从一级缓存中取,没有,线程二进二级缓存找A发现有锁,等待
3.然后线程一继续进行属性赋值,发现有依赖B,继续getBean B,B从一级缓存取没有,B实例化后加入二级缓存
,需要属性赋值,再getBean A ,发现一级缓存中没有,进入二级缓存有,直接返回A,B就实例化好了,此时A就实例化完成了,一级缓存中新增完整A
,二级缓存中移除了A
4.锁解除了,线程二继续第二步的操作,进二级缓存找A,发现二级缓存中没有A,然后再走一遍,这里性能也有问题
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
private final Map<String,Object> earlySingletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
private void refresh()throws Exception{
// 解析配置,注册BeanDefinition
loadBeanDefinitions();
// 创建所有单例Bean
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
// 创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
// 创建所有的bean
private void finishBeanFactoryInitialization()throws Exception{
//
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
/// ***********修改的地方**************从上面移到下面这里只锁二级缓存
// 如果没有就开始创建
synchronized(singletonObjects){
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);///这里在bean实例化后就加入一级缓存,可以解决循环依赖问题
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
earlySingletonObjects.remove(beanName);
return bean;
}
}
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
synchronized(earlySingletonObjects){
if(earlySingletonObjects.containsKey(beanName)){
return earlySingletonObjects.get(beanName);
}
}
}
问题解决
1.需要加入双重锁检查机制
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
synchronized(singletonObjects){
...
// 双重判断
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
...
return bean;
}
}
5.后面就是加AOP的话需要继续修改,需要加入三级缓存,待补充
public class AnnotationConfigApplicationContext{
private Map<String,BeanDefinition> beanDefinitonMap = new LinkedHashMap<String,BeanDefinition>();
private final Map<String,Object> singletonObjects = new ConcurrentHashMap<>();
private final Map<String,Object> earlySingletonObjects = new ConcurrentHashMap<>();
public AnnotationConfigApplicationContext() throws Exception{
// 加载ioc容器,创建所有bean
refresh();
}
}
private void refresh()throws Exception{
// 解析配置,注册BeanDefinition
loadBeanDefinitions();
// 创建所有单例Bean
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
// 创建A BeanDefinition
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
// 创建B BeanDefinition
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
// 创建所有的bean
private void finishBeanFactoryInitialization()throws Exception{
//
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
// 根据beanName 创建一个个bean
private Object getBean(String beanName)throws Exception{
// 判断bean是否已经创建好了
Object bean = getSingleton(beanName);
// 如果已经创建好就直接返回
if(bean!=null){
return bean;
}
/// ***********修改的地方**************从上面移到下面这里只锁二级缓存
// 如果没有就开始创建
synchronized(singletonObjects){
// ***********修改的地方**************双重判断
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
// 1.实例化 反射
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);///这里在bean实例化后就加入一级缓存,可以解决循环依赖问题
// 2.属性注入
Filed[] declareFields = beanClass.getDeclareFields();
for(Filed filed:declareFields){
Autowired autowired = filed.getAnnotation(Autowired.class);
if(autowired!=null){
String name = filed.getName();
Object depBean = getBean(name ) ;
filed.setAccessible(true);
filed.set(bean,depBean);
}
}
// 3.初始化
// 4.将bean放入一级缓存
singletonObjects.put(beanName,bean);
earlySingletonObjects.remove(beanName);
return bean;
}
}
// 判断bean是否创建
private void getSingleton(String beanName){
//
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
synchronized(earlySingletonObjects){
if(earlySingletonObjects.containsKey(beanName)){
return earlySingletonObjects.get(beanName);
}
}
}