spring 注解含义

1、@RequestMapping 是定义控制器对应的请求参数的。

[java]  view plain copy
  1. /** 
  2.  * 2010-1-25 
  3.  */  
  4. package org.zlex.spring.controller;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.ui.ModelMap;  
  9. import org.springframework.web.bind.annotation.ModelAttribute;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.zlex.spring.domain.Account;  
  13. import org.zlex.spring.service.AccountService;  
  14.   
  15. /** 
  16.  * 登录控制器 
  17.  *  
  18.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
  19.  * @version 1.0 
  20.  * @since 1.0 
  21.  */  
  22. @Controller  
  23. @RequestMapping(value = "/login.do")  
  24. public class LoginController {  
  25.   
  26.     @Autowired  
  27.     private AccountService accountService;  
  28.   
  29.     /** 
  30.      * 初始化表单 
  31.      *  
  32.      * @param model 
  33.      * @return 
  34.      */  
  35.     @RequestMapping(method = RequestMethod.GET)  
  36.     public String initForm(ModelMap model) {  
  37.         Account account = new Account();  
  38.         model.addAttribute("account", account);  
  39.         // 直接跳转到登录页面  
  40.         return "account/login";  
  41.     }  
  42.   
  43.     /** 
  44.      * 登录 
  45.      *  
  46.      * @param account 
  47.      * @return 
  48.      */  
  49.     @RequestMapping(method = RequestMethod.POST)  
  50.     public String login(@ModelAttribute("account") Account account) {  
  51.         Account acc = accountService.read(account.getUsername(), account  
  52.                 .getPassword());  
  53.         if (acc != null) {  
  54.             return "redirect:profile.do?id=" + acc.getId();  
  55.         } else {  
  56.             return "redirect:login.do";  
  57.         }  
  58.     }  
  59. }  
@RequestMapping(value = "/login.do")定义LoginController的访问地址。

@RequestMapping可以注释在类上也可以住是在方法上,注释在类上是定义该类的访问地址,需要访问其方法是,需要在地址的后面加上”?“+方法名。如果该注解注释在方法上就指定了该方法的访问地址。它是由一些参数,我用到的分别是mtehod -指定请求的类型即GET/POST。

@ModelAttribute("account")用来绑定表单即指明了这个方法使用的数据是来自account这个表单的数据,接收数据的对象就是Account


2、@Scope("prototype") 在类上使用该注解,表示该类使用范围

@Scope("prototype")
public class AccountAction extends BaseAction{

.............

}

scope="prototype"来保证每一个请求有一个单独的Action来处理, 避免struts中Action的线程安全问题

即spring 默认scope 是单例模式,这样只会创建一个Action对象,每次访问都是同一个Action对象,数据不安全,struts2 是要求 每次次访问 都对应不同的Action,scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象。


3、@Autowried

@Autowried 注释在成员变量上spring就会将其以Byname的形式自动化注入到spring容器中,因此便省去了get/set方法和spring配置文件的配置。

@Autowried 注释在成员方法中,spring就会把方法的变量将方法的参数注入进来,构造方法的注释同样。

2.@Qualifer  标注的对象与上一个相同,不同的是它指定了注入对象的类型,在使用@Autowried时如果没有指定注入参数的类型就会报错,因此@Qualifer是对@Autowried的有益补充。

[java]  view plain copy
  1. @Autowired    
  2. public void setUserDao(@Qualifier("userDao") UserDao userDao) {     
  3.     this.userDao = userDao;     
  4. }    

如果没有指定userDao 的类型的,并且存在另一个名字为userDao的对象,那么是spring就不知道该注入哪一个对象,如果使用@Qualifer指定注入对象的类型的话就不会出现保存的现象。


4、@Resource

与autowired使用方式相同,不同的是@Autowried是通过Bytype的方式注入spring的,而@Resource是通过Byname或Bytype方式注入spring的


5. @Component(不推荐使用)、@Repository、@Service、@Controller
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
Java代码
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    ...
}
使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称:
@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值