整理自网上:
web
Annotation | Package Detail/Import statement |
---|---|
@Service | import org.springframework.stereotype.Service; |
@Repository | import org.springframework.stereotype.Repository; |
@Component | import org.springframework.stereotype.Component; |
@Autowired | import org.springframework.beans.factory.annotation.Autowired; |
@Transactional | import org.springframework.transaction.annotation.Transactional; |
@Scope | import org.springframework.context.annotation.Scope; |
Spring MVC Annotations | |
@Controller | import org.springframework.stereotype.Controller; |
@RequestMapping | import org.springframework.web.bind.annotation.RequestMapping; |
@PathVariable | import org.springframework.web.bind.annotation.PathVariable; |
@RequestParam | import org.springframework.web.bind.annotation.RequestParam; |
@ModelAttribute | import org.springframework.web.bind.annotation.ModelAttribute; |
@SessionAttributes | import org.springframework.web.bind.annotation.SessionAttributes; |
Spring Security Annotations | |
@PreAuthorize | import org.springframework.security.access.prepost.PreAuthorize; |
@Service、@Repository、@Controller、@Component 这四个都是用来注解spring的bean,站在程序的角度它们都是等效。但从名字上,咱们很容易看出@Service是注解业务层的bean – Service,@Repository是注解持久层的bean – Dao,@Controller是注解MVC的Controller,@Component 用来注解普通的bean,当这个bean很差归类的时候,就用@Component。spring
自动注入值,以下自动注入companyDAO。前提你要保证companyDAO存在spring的context里。session
添加@Transactional到某个Service类上,说明该Service的全部方法都支持事务管理,若在某个方法上添加@Transactional,只声明该方法支持事务。当支持事务的方法开始执行前都会先打开一个事务,碰到异常时就会回滚。Spring的默认配置是碰到RunTimeException时才会进行事务回滚。app
对应<bean scope=”prototype”/>里的scope,它的值有singleton、prototype、request,session,global session和自定义模式。post
在类或方法上面使用此注解,设置URL访问地址。它有两个属性,value指定访问路径,method指定指定请求方式,请求方式在RequestMethod这个类中,所有以常量形式定义,它默认使用GET请求。它也能够只指定访问路径,以下所示spa
以下{context.path}/comany/addCompany 映射到addCompany方法。prototype
获取URL访问路径变量。在下面的例子里,访问路径是/company/techferry,companyName获取到的值就是techferry。code
把URL上的参数绑定到Controller方法上的参数,pageNum的值就是来源于request.getParameter(“pageNum”)。orm
当参数不少的时候,直接定义在方法上,方法的代码会很长,很是丑陋。一般的作法是定义一个formbean,而后在formbean前使用@ModelAttribute注解。Spring MVC会自动把URL的参数根据匹配的字段名一个一个的设置到formbean里。blog
声明一个变量,存放在session里,多个请求之间能够共享操做这个变量。
权限验证
以下例子,只有用户的role包含“ROLE_ADMIN”才能够删除Contact。