[置顶] JAVA元数据编程零接触 -- 实现简单的MVC跳转控制雏形

来源:互联网 发布:人工智能能否代替老师 编辑:程序博客网 时间:2024/06/01 09:44

之前自己写的FLEAMVC框架虽然实现了0配置的问题,但是在MVC的实现上问题颇多,最近看了下元数据编程.也理清了思路...

 

对于控制器 它不是自己调用的,而是由core核心来调用的 , 这里贴出一个刚刚写的雏形代码...

 

package cn.iamsese.www.webdev.annotation;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(value=RetentionPolicy.RUNTIME)public @interface ToView {public String url() default "http://iamsese.cn/index.do" ;}

 

 

package cn.iamsese.www.webdev.annotation;public class MC {@ToView(url="http://iamsese.cn/iamsese.do")public void listAction(){System.out.println("操作list方法");}}

 

 

 

package cn.iamsese.www.webdev.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Method;public class Test {public void interop(Method met){Annotation[] ann = met.getAnnotations();if (met.isAnnotationPresent(ToView.class)){ToView annserv = (ToView)met.getAnnotation(ToView.class);String url = annserv.url();String metname = met.getName();System.out.println("[" + metname + "/" + url + "]");}} public void _do(String ctr , String act) throws Exception{Class <?> cls = Class.forName("cn.iamsese.www.webdev.annotation." + ctr) ;Method met1 = cls.getMethod(act + "Action");met1.invoke(cls.newInstance(), null);this.interop(met1);}public static void main(String[] args) throws Exception {(new Test())._do("MC", "list");}}

 

 

 

Spring 2.5似乎完成了这种功能,尚未接触,这里将所需文件上传至此,嘿嘿

 

  • 大小: 23.8 KB
  • baseAnnotationIocOfSpring.rar (2.8 MB)
  • 下载次数: 15
  • 查看图片附件
0 0