熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring2.5注釋驅動與基於注釋的MVC

2013-11-23 20:33:13  來源: Java開源技術 

  Spring
                   Foojava FooStringname
                  package comtonytest;
    publicclass Foo {
            private String name;
                public String toStirng(){
                   returnFoo Name is : + thisname;
                }
    Setget
    }
    Barjava BarStringadd
    package comtonytest;
    publicclass Bar {
            private String add;
                public String toStirng(){
                   returnBar Add is : + thisadd;
                }
    Setget
    }
    Mainjava MainFooBar
    package comtonytest;
    publicclass Main {
            private Foo foo;
            private Bar bar;
                public String toString(){
    returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];
                }
    Setget
    }
    Springconfigbeansxml
            <bean id=main class=comtonytestMain>
               <property name=foo ref=foo></property>
               <property name=bar ref=bar></property>
            </bean>

  <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=bar class=comtonytestBar>
               <property name=add value=Bar></property>
    </bean>
    Testjava TestSpringmain
    package comtonytest;
    import orgntextApplicationContext;
    import orgntextsupport
    ClassPathXmlApplicationContext;
    publicclass Test {
                publicstaticvoid main(String[] args) {
                     String[] locations = {springconfigbeansxml};
    ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
                     Main main = (Main) ctxgetBean(main);
                     systemoutprintln(main);
                }
    }
    Test
    Main : [Foo Name is :Foo Bar Add is :Bar]
    )Spring package comtonytest;

  import orgspringframeworkbeansfactoryannotationAutowired;

  publicclass Main {
               @Autowired
               private Foo foo;
               @Autowired
               private Bar bar;

  public String toString(){
 

  returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];
               }
    }
    Spring
    <!  BeanPostProcessor  @Autowired  Bean  >
    <bean class=orgspringframeworkbeansfactoryannotation
    AutowiredAnnotationBeanPostProcessor/>
            <!— main Bean  >
            <bean id=main class=comtonytestMain></bean>

  <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=bar class=comtonytestBar>
               <property name=add value=Bar></property>
    </bean>
    )Spring        @Qualifier(foo)
    publicclass Main {
               @Autowired
               @Qualifier(foo)
               private Foo foo;

  @Autowired
               private Bar bar;

  public String toString(){
    returnMain : [ + thisfootoStirng() + + thisbartoStirng() + ];
               }
    }
    <bean class=orgspringframeworkbeansfactoryannotation
    AutowiredAnnotationBeanPostProcessor/>

  <bean id=main class=comtonytestMain></bean>

  <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=bar class=comtonytestBar>
               <property name=add value=Bar></property>
    </bean>
    Testjava:
    Main : [Foo Name is :Foo Bar Add is :Bar]
    Springfoomain
    )<context:annotationconfig/>Spring <context:annotationconfig/>
    <context:annotationconfig/>
            <bean id=main class=comtonytestMain></bean>

  <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=foo class=comtonytestFoo>
               <property name=name value=Foo></property>
            </bean>
            <bean id=bar class=comtonytestBar>
               <property name=add value=Bar></property>
    </bean>
    <bean class=orgspringframeworkbeansfactoryannotation
    AutowiredAnnotationBeanPostProcessor/>
    <context:annotationconfig/>
    <context:annotationconfig/> ) @Component
    publicclass Foo {
               private String name = Foos name;
               public String toStirng(){
                  returnFoo Name is : + thisname;
               }
    }
    @ComponentBean
    Mainjava
    @Component(main)
    publicclass Main {
               @Autowired
               private Foo foo;

  @Autowired
        private Bar bar;
    ……
    @Component <context:componentscan basepackage=comtonytest/>
           SpringSpring 也為 Spring MVC引入了注釋驅動功能現在我們無須讓Controller繼承任何接口無需在XML配置文件中定義請求和Controller的映射關系僅僅使用注釋就可以讓一個POJO具有Controller的絕大部分功能 —— Spring MVC框架的易用性得到了進一步的增強
    ) 基於注解的
    由於Spring MVC的Controller必須事先是一個Bean所以 @Controller注解是不可缺少的請看下面的代碼清單
    代碼清單
                  @Controller //Controller

  publicclass FooController {
                @Autowired
                private FooService fooService;
                @RequestMapping(/listdo) //URL
                public String[] list() {
              String[] list = fooServicegetAll();
                 systemoutprintln(list);
                 return list;
                }
                @RequestMapping(/deldo) //URL
    publicvoid del(HttpServletRequest request HttpServletResponse response) {
                   fooServicedoDel(requestgetParameter(id));
                }
    }
    @ControllerFooControllerjavaFooControllerjavaURLURL
    ) 一個Controller對應一個URL由請求參數決定請求處理方法
   
    @Controller
    @RequestMapping(/doFoodo)// URL
    publicclass FooController {
               @Autowired
               private FooService fooService;
               //listURL /doFoodo?mode=list
               @RequestMapping(params = mode=list)
                public String[] list() {
                   String[] list = fooServicegetAll();
                    systemoutprintln(list);
                    return list;
                }
               //delURL /doFoodo?mode=del
               @RequestMapping(params = mode=del)
    publicvoid del(HttpServletRequest request
    HttpServletResponse response) {
                  fooServicedoDel(requestgetParameter(id));
                }
    }
    讓請求處理方法處理特定的HTTP請求如POST類型的請看下面的代碼清單
    ) 讓請求處理方法處理特定的 HTTP 請求方法
    @Controller
    @RequestMapping(/doFoodo)// URL
    publicclass FooController {
               //POST
            @RequestMapping(params = mode=submit
    method = RequestMethodPOST)
               public String submit(HttpServletRequest request
                   HttpServletResponse response){
                  systemoutprintln( submit );
                  returnsuccess;
               }
    }
    submitPOSTURL
    ) 處理方法入參綁定 URL 參數
    代碼清單
    @Controller
    @RequestMapping(/doFoodo)// URL
    publicclass FooController {
               @Autowired
               private FooService fooService;

  //delURL /doFoodo?mode=del&id=
               @RequestMapping(params = mode=del)
                public String del(int id) {
                  fooServicedoDel(id);
                  returnsuccess;
                }
    }
    /doFoodo?mode=del&id=URL
    Spring del() id del() id del() String Spring ModelAndView Spring MVC
   
    @Controller
    @RequestMapping(/doFoodo)// URL
    publicclass FooController {
               @Autowired
               private FooService fooService;

  //delURL /doFoodo?mode=del&id=
               @RequestMapping(params = mode=del)
                public String del(@RequestParam(id)int id) {
                  fooServicedoDel(id);
                  returnsuccess;
                }
    }
    del() id @RequestParam(id) id URL


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28654.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.