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{
refresh();
}
}
private void refresh()throws Exception{
loadBeanDefinitions();
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
private void finishBeanFactoryInitialization()throws Exception{
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
private Object getBean(String beanName)throws Exception{
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
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);
}
}
singletonObjects.put(beanName,bean);
return 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();
上面代码有循环依赖问题,解决需要修改如下代码
...
bean = beanClass.newInstance();
singletonObjects.put(beanName,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{
refresh();
}
}
private void refresh()throws Exception{
loadBeanDefinitions();
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
private void finishBeanFactoryInitialization()throws Exception{
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
private Object getBean(String beanName)throws Exception{
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);
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);
}
}
singletonObjects.put(beanName,bean);
return 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锁起来:
private Object getBean(String beanName)throws Exception{
synchronized(singletonObjects){
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{
refresh();
}
}
private void refresh()throws Exception{
loadBeanDefinitions();
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
private void finishBeanFactoryInitialization()throws Exception{
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
private Object getBean(String beanName)throws Exception{
synchronized(singletonObjects){
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);
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);
}
}
singletonObjects.put(beanName,bean);
return 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{
refresh();
}
}
只锁二级缓存,不锁一级缓存
private Object getBean(String beanName)throws Exception{
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
synchronized(singletonObjects){
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
earlySingletonObjects .put(beanName,bean);
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);
}
}
singletonObjects.put(beanName,bean);
earlySingletonObjects.remove(beanName);
return 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{
refresh();
}
}
private void refresh()throws Exception{
loadBeanDefinitions();
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
private void finishBeanFactoryInitialization()throws Exception{
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
private Object getBean(String beanName)throws Exception{
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
synchronized(singletonObjects){
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);
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);
}
}
singletonObjects.put(beanName,bean);
earlySingletonObjects.remove(beanName);
return 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.需要加入双重锁检查机制
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{
refresh();
}
}
private void refresh()throws Exception{
loadBeanDefinitions();
finishBeanFactoryInitialization();
}
private void loadBeanDefinitions()throws Exception{
RootBeanDefinition aBeanDefinition = new RootBeanDefinition(InstanceA.class);
RootBeanDefinition bBeanDefinition = new RootBeanDefinition(InstanceB.class);
beanDefinitonMap.put("instanceA",aBeanDefinition);
beanDefinitonMap.put("instanceB",bBeanDefinition);
}
private void finishBeanFactoryInitialization()throws Exception{
for(String beanDefinitonMap.keySet()){
getBean(beanName);
}
}
private Object getBean(String beanName)throws Exception{
Object bean = getSingleton(beanName);
if(bean!=null){
return bean;
}
synchronized(singletonObjects){
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
RootBeanDefinition beanDefinition = (RootBeanDefinition)beanDefinitonMap.get(beanName);
Class<?> beanClass = beanDefinition.getBeanClass();
bean = beanClass.newInstance();
singletonObjects.put(beanName,bean);
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);
}
}
singletonObjects.put(beanName,bean);
earlySingletonObjects.remove(beanName);
return bean;
}
}
private void getSingleton(String beanName){
if(singletonObjects.containsKey(beanName)){
return singletonObjects.get(beanName);
}
synchronized(earlySingletonObjects){
if(earlySingletonObjects.containsKey(beanName)){
return earlySingletonObjects.get(beanName);
}
}
}
|