1、@RequestMapping 是定义控制器对应的请求参数的。
- /**
- * 2010-1-25
- */
- package org.zlex.spring.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.zlex.spring.domain.Account;
- import org.zlex.spring.service.AccountService;
- /**
- * 登录控制器
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
- * @version 1.0
- * @since 1.0
- */
- @Controller
- @RequestMapping(value = "/login.do")
- public class LoginController {
- @Autowired
- private AccountService accountService;
- /**
- * 初始化表单
- *
- * @param model
- * @return
- */
- @RequestMapping(method = RequestMethod.GET)
- public String initForm(ModelMap model) {
- Account account = new Account();
- model.addAttribute("account", account);
- // 直接跳转到登录页面
- return "account/login";
- }
- /**
- * 登录
- *
- * @param account
- * @return
- */
- @RequestMapping(method = RequestMethod.POST)
- public String login(@ModelAttribute("account") Account account) {
- Account acc = accountService.read(account.getUsername(), account
- .getPassword());
- if (acc != null) {
- return "redirect:profile.do?id=" + acc.getId();
- } else {
- return "redirect:login.do";
- }
- }
- }
@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的有益补充。
- @Autowired
- public void setUserDao(@Qualifier("userDao") UserDao userDao) {
- this.userDao = userDao;
- }
如果没有指定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。