自己动手实现IOC和MVC(四)

现在我要进行实例化的信息都收集起来,然后进行实例化了

问题?

①怎么进行实例化?

  这时候我要用到我们收集的class的信息类的List,进行循环 ,然后利用java反射机制进行实例化操作

  下面一段伪代码:

  

 List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
		 if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
				for(BeanDefinition bd:beanDefinitions){
					if(bd!=null){
						//初始化bean
						//然后是存储bean
					}
				}
		}

②怎么进行注入?

也是循环List<BeanDefinition> ,然后循环BeanDefinition里面的属性集合,接着利用java反射调用set方法进行注入操作

List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
		for(BeanDefinition bd : beanDefinitions){
			Object bean =获得实例化后的bean根据id
			List<PropertyDefinition> pds = bd.getProperties();
			if(pds!=null && !pds.isEmpty()){
				for(PropertyDefinition pd:pds){
					String name = pd.getName();
					String ref = pd.getRef();
					if(ref!=null && !"".equals(ref)){
						//这里会进行属性的注入操作
					}
				}
			}
		}

②实例化之后放在哪里?

会放在一个Map里 ,供我们以后进行使用

这里我们提供一个类用于对这个Map的操作 ,具体实现如下:

package com.ajunframework.beans.factory;

import java.util.HashMap;
import java.util.Map;

/**
 * 存储bean
 * @author ajun
 *
 */
public class BeanMap {

	private static Map<String,Object> beans = new HashMap<String,Object>();
	
	private BeanMap(){}
	/**
	 * 添加bean
	 * @param beanName
	 * @param value
	 */
	public  static void put(String beanName ,Object value){
		beans.put(beanName, value);
	}
	
	/**
	 * 获得bean
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName){
		if(!beans.isEmpty()){
			return beans.get(beanName);
		}
		return new HashMap<String,Object>();
	}
	
	public static Map<String,Object> getBeanMap(){
		return beans;
	}
}

此时提供了一个BeanFactory interface 用于我们扩展 对beanMap的操作

package com.ajunframework.beans.factory;

/**
 *bean工厂
 * @author ajun
 * @http://blog.youkuaiyun.com/ajun_studio
 */
public interface BeanFactory {

	/**
	 * 设置bean
	 * @param beanName 
	 * @param fullClassName
	 */
	 void setBean(String beanName,String fullClassName);
	/**
	 * 获得bean根据beanName
	 * @param name
	 * @return
	 */
	Object getBean(String name);
	
	<T> T getBean(String name, Class<T> requiredType);
	
	<T> T getBean(Class<T> requiredType);
	
	Object getBean(String name, Object... args);
	
	boolean containsBean(String name);
	
	boolean isSingleton(String name);
	
	boolean isPrototype(String name);
	
	@SuppressWarnings("unchecked")
	boolean isTypeMatch(String name, Class targetType);
	
	Class<?> getType(String name);
	
	String[] getAliases(String name);
	
}

下面是实现类 AnnotationBeanFactory.java

package com.ajunframework.beans.factory;


import com.ajunframework.beans.annotation.Action;
import com.ajunframework.beans.annotation.Dao;
import com.ajunframework.beans.annotation.Scope;
import com.ajunframework.beans.annotation.Service;
import com.ajunframework.beans.constant.BeanScop;
import com.ajunframework.beans.utils.BeanUtils;

/**
 * bean工厂
 * @author ajun
 * @http://blog.youkuaiyun.com/ajun_studio
 */
public class AnnotationBeanFactory implements BeanFactory {

	private static AnnotationBeanFactory beanFactory = new AnnotationBeanFactory();
	
	private AnnotationBeanFactory(){}
	
	public static AnnotationBeanFactory getBeanFactory(){
		if(beanFactory!=null){
			return beanFactory;
		}
		return new AnnotationBeanFactory();
	}
	
	public void setBean(String beanName,String fullClassName) {
		try {
			Object bean = BeanUtils.instanceClass(Class.forName(fullClassName));
			BeanMap.put(beanName, bean);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}

	public boolean containsBean(String name) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			return true;
		}
		return false;
	}


	public Object getBean(String name) {
		return BeanMap.getBeanMap().get(name);
	}

	@SuppressWarnings("unchecked")
	public <T> T getBean(String name, Class<T> requiredType) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			Class<?> clazz = bean.getClass();
			if(clazz.getName().equals(requiredType.getName()) ){
				return (T)bean;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public <T> T getBean(Class<T> requiredType) {
		if(requiredType.isAnnotationPresent(Dao.class) || requiredType.isAnnotationPresent(Service.class) || requiredType.isAnnotationPresent(Action.class)){
			String fullName = requiredType.getName();
			String beanName = fullName.substring(fullName.lastIndexOf(".")+1).substring(0,1).toLowerCase()+ fullName.substring(fullName.lastIndexOf(".")+1).substring(1);
			Object bean = BeanMap.getBeanMap().get(beanName);
			return (T)bean;
		}
		
		return null;
	}

	public Object getBean(String name, Object... args) {
		
		return null;
	}

	public Class<?> getType(String name) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			bean.getClass().getClass();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public boolean isPrototype(String name) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			Class clazz = bean.getClass();
			if(clazz.isAnnotationPresent(Scope.class)){
				String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
				if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
					return true;
				}
			}
		}
		
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isSingleton(String name) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			Class clazz = bean.getClass();
			if(clazz.isAnnotationPresent(Scope.class)){
				String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
				if(scopeValue!=null && BeanScop.SINGLETON.equals(scopeValue.trim())){
					return true;
				}else if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
					return false;
				}else{
					return true;
				}
			}else{
				return true;
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean isTypeMatch(String name, Class targetType) {
		Object bean = BeanMap.getBeanMap().get(name);
		if(bean!=null){
			Class clazz = bean.getClass();
			if(clazz.equals(targetType)){
				return true;
			}
		}
		return false;
	}

	public String[] getAliases(String name) {
		return null;
	}

}

下面给出上面整个过程的代码:

初始化上下文bean 接口

package com.ajunframework.beans.applicationContext;


/**
 * 初始化上下文bean 接口
 * @author ajun
 * @http://blog.youkuaiyun.com/ajun_studio
 */
public interface  ClassPathApplicationContext {

	/**
	 * instance bean
	 */
	 void instanceBean();
	
	
	/**
	 * DI bean
	 */
	 void injectObject();
	
	/**
	 * read class‘s Annotation info to instance definitionBean
	 */
	 void readAnnotationCLass();
	
	 /**
	  * 初始化
	  */
	 void init();
}

基于注释的上下文实例化bean类 AnnotationClassPathApplicationContext.java

package com.ajunframework.beans.applicationContext;


import java.util.List;

import com.ajunframework.beans.annotation.Action;
import com.ajunframework.beans.annotation.Dao;
import com.ajunframework.beans.annotation.Service;
import com.ajunframework.beans.definition.BeanDefinition;
import com.ajunframework.beans.definition.BeandefinitionList;
import com.ajunframework.beans.definition.PropertyDefinition;
import com.ajunframework.beans.factory.AnnotationBeanFactory;
import com.ajunframework.beans.scan.ScanClass;
import com.ajunframework.beans.utils.BeanWrapper;

/**
 * 基于注释的上下文实例化bean类
 * @author ajun
 * @http://blog.youkuaiyun.com/ajun_studio
 */
public class AnnotationClassPathApplicationContext implements ClassPathApplicationContext {

	private static AnnotationClassPathApplicationContext cx = new AnnotationClassPathApplicationContext();
	
	private AnnotationClassPathApplicationContext(){}
	
	public static AnnotationClassPathApplicationContext getAnnotationClassPathApplicationContext(){
		if(cx!=null){
			return cx;
		}
		return new AnnotationClassPathApplicationContext();
	}
	
	public void instanceBean() {
		 List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
		 if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
				for(BeanDefinition bd:beanDefinitions){
					if(bd!=null){
						//初始化bean
						AnnotationBeanFactory.getBeanFactory().setBean(bd.getId(), bd.getCalssName());
					}
				}
		}
		
	}

	public void injectObject() {
		List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
		for(BeanDefinition bd : beanDefinitions){
			Object bean = AnnotationBeanFactory.getBeanFactory().getBean(bd.getId());
			List<PropertyDefinition> pds = bd.getProperties();
			if(pds!=null && !pds.isEmpty()){
				for(PropertyDefinition pd:pds){
					String name = pd.getName();
					String ref = pd.getRef();
					if(ref!=null && !"".equals(ref)){
						BeanWrapper b = new BeanWrapper(bean);
						Object propvalue = AnnotationBeanFactory.getBeanFactory().getBean(name);
						b.setPropertyValue(name, propvalue);
					}
				}
			}
		}
		
	}

	public void readAnnotationCLass() {
		Class<?> [] classes = ScanClass.getScanPackageClasses();
		if(classes != null && classes.length>0){
			for(Class<?> c:classes){
				if(c.isAnnotationPresent(Dao.class) || c.isAnnotationPresent(Service.class) || c.isAnnotationPresent(Action.class)){
					BeandefinitionList.addBeanDefinitionAndSetProperty(c);
				}
			}
		}
	}
	
	public  void init(){
		readAnnotationCLass();
		instanceBean();
		injectObject();
	}
	
}


到目前为止 整个ioc的过程已经完成了,下一节介绍一下这个project中用到的一些工具类



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Shark

感谢打赏,thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值