午后星期午

          使用Spring mvc 利用java的反射技術(shù),來(lái)掃描對(duì)應(yīng)包下的注解請(qǐng)求url 統(tǒng)一保存在數(shù)據(jù)庫(kù)中

          僅對(duì)  @requestMapping("/xxxx/其他參數(shù)")  的格式
           package cc.wshao.springmvc.util;
          import java.io.File;
          import java.lang.reflect.Method;
          import java.net.URL;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import cc.wshao.springmvc.service.RightService;
          public class RegRightUtils {
          private static final String rootUrl = "/spring";
          private static RightService rightService;
          public static void main(String[] args) throws Exception {
          ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            rightService = context.getBean(RightService.class);
          ClassLoader classLoader = RegRightUtils.class.getClassLoader();
          URL url = classLoader.getResource("com/er07/book/springmvc/controller");
          String rootPath = url.getPath().toString();
          File rootFile = new File(rootPath);
          File [] files = rootFile.listFiles();
          for(File f: files) {
          System.err.println(f.getName());
          String className = f.getName().substring(0, f.getName().indexOf(".class"));
          Class clazz = Class.forName("com.er07.book.springmvc.controller."+className);
          parseClass(clazz);  //解析  出url
          }
           
          }
          public  static void parseClass(Class clazz) {
           
          if(clazz.isAnnotationPresent(Controller.class)) {   //是否為一個(gè)controller ? 
          String classUrl ="";
           if(clazz.isAnnotationPresent(RequestMapping.class)) {
           RequestMapping requestMapping_clazz = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            classUrl = requestMapping_clazz.value()[0];
            
            if(classUrl.equals("/")) {  //如果是"/" 的話  制空    
            classUrl ="";
            }
           }
           Method [] ms = clazz.getDeclaredMethods();
           
           for(Method m :ms) {
           
           if(m.isAnnotationPresent(RequestMapping.class)) {
           RequestMapping requestMapping_method = m.getAnnotation(RequestMapping.class);
           String methodUrl = requestMapping_method.value()[0];
           
             int  index = methodUrl.indexOf("/");
             index = methodUrl.indexOf("/",index+1);
             if(index!=-1) {  //如果存在 則進(jìn)行截取前面的url 
             
              methodUrl = methodUrl.substring(0, index);
             }
           String resultUrl = rootUrl+classUrl+methodUrl;
           rightService.appendRightByUrl(resultUrl);
           }
           }
          }
          }
          }
          //實(shí)現(xiàn)方法: 
          public void appendRightByUrl(String url) {
          //查詢 此url 在數(shù)據(jù)庫(kù)中是否存在  此權(quán)限
          String hql = "from Right where rightUrl =?";
          Right r = (Right) this.rightDao.uniqueResult(hql, url);
          if(r==null) { //不存在 , 插入數(shù)據(jù)庫(kù)中
          Right newRight = new Right();
          newRight.setRightUrl(url);
          this.saveOrUpdateRight(newRight);
          }
          }

          posted on 2014-03-25 14:54 午后星期午 閱讀(1250) 評(píng)論(0)  編輯  收藏 所屬分類: JavaEE

          主站蜘蛛池模板: 大丰市| 高唐县| 来安县| 拉孜县| 大城县| 商城县| 灵宝市| 毕节市| 台东县| 贡山| 盐城市| 岳阳县| 泗水县| 威远县| 濮阳市| 常德市| 南陵县| 禹城市| 晋中市| 海伦市| 前郭尔| 惠东县| 宝应县| 略阳县| 大埔县| 长阳| 丰都县| 梅河口市| 九龙县| 灵石县| 伊川县| 泊头市| 宁海县| 蓬安县| 伊吾县| 嘉义市| 根河市| 乌兰察布市| 凉城县| 太湖县| 梅河口市|