上一节中可以扫描指定package以及子package下的所有class了,这一节我们会介绍怎么收集和整理这些扫描到得class的信息
这时候我们整理我扫描到得的class的信息并将其赋值给我们自己定义的一个class
问题如下:
①怎么定义扫描到class的信息的类呢?
首先我要实例化的bean的id必须是唯一的,这个id你可以在你要进行instance的class(如 UserDao.java) 上进行annotation的,这里默认采用实例化类的名字 ,首字母小写(userDao),下面具体实现
BeanDefinition.java(用于收集实例化bean的类)
package com.ajunframework.beans.definition;
import java.util.ArrayList;
import java.util.List;
/**
* 读取注释bean的初始化属性
* @author ajun
* @http://blog.youkuaiyun.com/ajun_studio
*/
public class BeanDefinition {
private String id;//默认为class的名字
private String calssName;//com.ajun.bean.AjunClass
//用于实现依赖注入
private List<PropertyDefinition> properties = new ArrayList<PropertyDefinition>();
public BeanDefinition(String id, String calssName) {
super();
this.id = id;
this.calssName = calssName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCalssName() {
return calssName;
}
public void setCalssName(String calssName) {
this.calssName = calssName;
}
public List<PropertyDefinition> getProperties() {
return properties;
}
public void setProperties(List<PropertyDefinition> properties) {
this.properties = properties;
}
}
用于收集注入bean的属性的信息类
package com.ajunframework.beans.definition;
/**
* 注入属性的定义
* @author ajun
* @http://blog.youkuaiyun.com/ajun_studio
*/
public class PropertyDefinition {
private String name;//注入的属性的名字 默认为属性名字
private String ref;//注入到哪个bean中,bean的id
public PropertyDefinition(String name, String ref) {
this.name = name;//注入属性的名字
this.ref = ref;//注入到哪个bean中 ,这个表示bean的id
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
}
②怎么进行收集并赋值给我们定义信息类
因为上一节中我们提到过 ,有一个辅助我们扫描的工具类 ,返回我要扫描的类的class数组,此时我们就要在这个class数组上下手了,然后创建BeanDefinition对象 ,一个扫描的类就对应一个BeanDefinition对象 ,此时我们会把我们扫描出来的class ,根据java annotation信息才实例化BeanDefinition对象 ,并且填充在一个List中 ,供我实例化应用。下面收集class信息的类的具体,请详细看注释
BeandefinitionList.java
package com.ajunframework.beans.definition;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.ajunframework.beans.annotation.Action;
import com.ajunframework.beans.annotation.Dao;
import com.ajunframework.beans.annotation.Property;
import com.ajunframework.beans.annotation.Service;
import com.ajunframework.beans.factory.RequestMapingMap;
import com.ajunframework.beans.utils.BeanUtils;
import com.ajunframework.exception.AjunIocException;
import com.ajunframework.servlet.annotation.RequestMapping;
/**
* 用于存储Beandefinition
* @author ajun
* @http://blog.youkuaiyun.com/ajun_studio
*/
public class BeandefinitionList {
//存储class信息类的集合
private static List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
/**
* 添加一个信息类
* @param bd
*/
public static void addBeanDefinition(BeanDefinition bd){
beanDefinitions.add(bd);
}
/**
* 添加一个信息类 根据类得全名
* @param fullClassName com.ajun.UserDao
*/
public static void addBeanDefinition(String id ,String fullClassName){
BeanDefinition bd = new BeanDefinition(id,fullClassName);
addBeanDefinition(bd);
}
/**
* 添加一个信息类并且导入其对应的属性信息类
* @param clazz
*/
public static void addBeanDefinitionAndSetProperty(Class<?> clazz){
String fullName = clazz.getName();
String id ="";
if(clazz.isAnnotationPresent(Dao.class)){
id = clazz.getAnnotation(Dao.class).value();
}else if(clazz.isAnnotationPresent(Service.class)){
id = clazz.getAnnotation(Service.class).value();
}else if(clazz.isAnnotationPresent(Action.class)){
id = clazz.getAnnotation(Action.class).value();
}
if(id==null || "".equals(id.trim())){
id = fullName.substring(fullName.lastIndexOf(".")+1).substring(0,1).toLowerCase()+ fullName.substring(fullName.lastIndexOf(".")+1).substring(1);
}
BeanDefinition bd = new BeanDefinition(id,fullName);
Field [] fields = BeanUtils.findDeclaredFields(clazz);
if(fields!=null && fields.length>0){
for(Field f:fields){
if(f.isAnnotationPresent(Property.class)){
PropertyDefinition pd = new PropertyDefinition(f.getName(),id);
bd.getProperties().add(pd);
}
}
}
Method [] methods = BeanUtils.findDeclaredMethods(clazz);
for(Method m : methods){
if(m.isAnnotationPresent(RequestMapping.class)){
String path = m.getAnnotation(RequestMapping.class).value();
if(RequestMapingMap.getBeanName(path)!=null){
throw new AjunIocException("RequestMapping's url is only ,now it is not only");
}
RequestMapingMap.put(path, id);
}
}
addBeanDefinition(bd);
}
//返回class信息类的List
public static List<BeanDefinition> getBeanDefinitions(){
return beanDefinitions;
}
}
现在我要进行实例化的信息都收集起来,接下来一节中我就可以进行实例化了