知识大全 Spring2.5注释驱动与基于注释的MVC
Posted 知
篇首语:天下之事常成于困约,而败于奢靡。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Spring2.5注释驱动与基于注释的MVC相关的知识,希望对你有一定的参考价值。
Spring2.5注释驱动与基于注释的MVC 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Spring Foo java FooStringname package tony test; publicclass Foo private String name; public String toStirng() return Foo Name is : + this name; Setget Bar java BarStringadd package tony test; publicclass Bar private String add; public String toStirng() return Bar Add is : + this add; Setget Main java MainFooBar package tony test; publicclass Main private Foo foo; private Bar bar; public String toString() return Main : [ + this foo toStirng() + + this bar toStirng() + ] ; Setget Spring config beans xml <bean id= main class= tony test Main > <property name= foo ref= foo ></property> <property name= bar ref= bar ></property> </bean>
<bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= bar class= tony test Bar > <property name= add value= Bar ></property> </bean> Test java TestSpringmain package tony test; import ntext ApplicationContext; import ntext support ClassPathXmlApplicationContext; publicclass Test publicstaticvoid main(String[] args) String[] locations = spring config beans xml ; ApplicationContext ctx = new ClassPathXmlApplicationContext(locations); Main main = (Main) ctx getBean( main ); system out println(main); Test Main : [Foo Name is :Foo Bar Add is :Bar] )Spring package tony test;
import springframework beans factory annotation Autowired;
publicclass Main @Autowired private Foo foo; @Autowired private Bar bar;
public String toString()
return Main : [ + this foo toStirng() + + this bar toStirng() + ] ; Spring <! BeanPostProcessor @Autowired Bean > <bean class= springframework beans factory annotation AutowiredAnnotationBeanPostProcessor /> <!— main Bean > <bean id= main class= tony test Main ></bean>
<bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= bar class= tony test Bar > <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() return Main : [ + this foo toStirng() + + this bar toStirng() + ] ; <bean class= springframework beans factory annotation AutowiredAnnotationBeanPostProcessor />
<bean id= main class= tony test Main ></bean>
<bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= bar class= tony test Bar > <property name= add value= Bar ></property> </bean> Test java: Main : [Foo Name is :Foo Bar Add is :Bar] Springfoo main )<context:annotation config/>Spring <context:annotation config/> <context:annotation config/> <bean id= main class= tony test Main ></bean>
<bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= foo class= tony test Foo > <property name= name value= Foo ></property> </bean> <bean id= bar class= tony test Bar > <property name= add value= Bar ></property> </bean> <bean class= springframework beans factory annotation AutowiredAnnotationBeanPostProcessor /> <context:annotation config/> <context:annotationconfig/> ) @Component publicclass Foo private String name = Foo s name ; public String toStirng() return Foo Name is : + this name; @Component Bean Main java @Component( main ) publicclass Main @Autowired private Foo foo;
@Autowired private Bar bar; …… @Component <context:ponent scan base package= tony test /> Spring Spring 也为 Spring MVC引入了注释驱动功能 现在我们无须让Controller继承任何接口 无需在XML配置文件中定义请求和Controller的映射关系 仅仅使用注释就可以让一个POJO具有Controller的绝大部分功能 —— Spring MVC框架的易用性得到了进一步的增强 ) 基于注解的 由于Spring MVC的Controller必须事先是一个Bean 所以 @Controller注解是不可缺少的 请看下面的代码清单 代码清单 @Controller //Controller
publicclass FooController @Autowired private FooService fooService; @RequestMapping( /list do ) //URL public String[] list() String[] list = fooService getAll(); system out println(list); return list; @RequestMapping( /del do ) //URL publicvoid del(HttpServletRequest request HttpServletResponse response) fooService doDel(request getParameter( id )); @ControllerFooController java FooController java URLURL ) 一个Controller对应一个URL 由请求参数决定请求处理方法 @Controller @RequestMapping( /doFoo do )// URL publicclass FooController @Autowired private FooService fooService; //listURL /doFoo do?mode=list @RequestMapping(params = mode=list ) public String[] list() String[] list = fooService getAll(); system out println(list); return list; //delURL /doFoo do?mode=del @RequestMapping(params = mode=del ) publicvoid del(HttpServletRequest request HttpServletResponse response) fooService doDel(request getParameter( id )); 让请求处理方法处理特定的HTTP请求如POST类型的 请看下面的代码清单 ) 让请求处理方法处理特定的 HTTP 请求方法 @Controller @RequestMapping( /doFoo do )// URL publicclass FooController //POST @RequestMapping(params = mode=submit method = RequestMethod POST) public String submit(HttpServletRequest request HttpServletResponse response) system out println( submit ); return success ; submitPOSTURL ) 处理方法入参绑定 URL 参数 代码清单 @Controller @RequestMapping( /doFoo do )// URL publicclass FooController @Autowired private FooService fooService;
//delURL /doFoo do?mode=del&id= @RequestMapping(params = mode=del ) public String del(int id) fooService doDel(id); return success ; /doFoo do?mode=del&id= URL Spring del() id del() id del() String Spring ModelAndView Spring MVC @Controller @RequestMapping( /doFoo do )// URL publicclass FooController @Autowired private FooService fooService;
cha138/Article/program/Java/ky/201311/28654相关参考
注释块标记 标记的顺序 块标记将采用如下顺序 … * *@param(classesinterfacesmethodsandconstructorsonly) *@retu
PHPWeb开发学习实录:PHP注释 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
GhostDoc自动生成XML注释 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!将XML注释迅速添
布林带(线)BOLL技术指标注释有哪些?原理:以移动平均线为中线,收盘价的均方差为带宽的轨道带。BOLL带绘制下轨线(LOWER)、上轨线(UPER)、中间线(MID)。中间线MID:收盘价的N日移动
PHPWeb开发学习实录:PHP注释运行结果 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n
PHPWeb开发学习实录:PHP注释实例应用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n
PHPWeb开发学习实录:PHP注释实例描述 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n
开源技术:Hibernate3注释简介 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 在过去几年
查看并导出SQLServer数据表中字段的注释信息 此示例为导出某个表注释的语句(表名是bbs_bank_log) SELECTsysobjectsnameAS表名syscolumnsname
Eclipse中文注释乱码解决 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!