自らIOCとMVCを実現する(3)
前のセクションでは、指定したpackageとサブpackageの下にあるすべてのclassをスキャンできます.このセクションでは、これらのスキャンから得られたclassの情報を収集し、整理する方法について説明します.
スキャンしたclassの情報を整理し、自分で定義したclassに値を付けます.
問題は次のとおりです.
①classにスキャンされた情報のクラスをどのように定義しますか?
まずインスタンス化するbeanのidは唯一でなければなりません.このidはinstanceのclass(UserDao.javaなど)でannotationを行うことができます.ここではデフォルトでインスタンス化クラスの名前、頭文字小文字(userDao)を採用しています.以下、具体的に実現します.
BeanDefinition.JAva(インスタンス化beanを収集するためのクラス)
②情報クラスを定義するための収集と付与方法
前節で述べたように、スキャンを支援するツールクラスがあり、スキャンするクラスのclass配列を返します.このclass配列に着手し、BeanDefinitionオブジェクトを作成します.スキャンするクラスはBeanDefinitionオブジェクトに対応します.このとき、スキャンしたclassをjava annotation情報に基づいてBeanDefinitionオブジェクトをインスタンス化します.そして、アプリケーションをインスタンス化するためにリストに記入します.次にclass情報を収集するクラスの詳細については、コメントを参照してください.
BeandefinitionList.java
ここでインスタンス化する情報を集めて、次のセクションでインスタンス化できます.
スキャンした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.csdn.net/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.csdn.net/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オブジェクトをインスタンス化します.そして、アプリケーションをインスタンス化するためにリストに記入します.次に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.csdn.net/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;
}
}
ここでインスタンス化する情報を集めて、次のセクションでインスタンス化できます.