按照注解的来源,也是分为3类
1. 内置注解 如@Override ,@Deprecated 等等
2. 第三方注解,如Hibernate, Struts等等
3. 自定义注解,如fahibernate的自定义注解
在工作中,大部分都使用的是第三方注解。那么接下来就介绍一些常用的第三方注解如:@RequestMapping、@ResponseBody、@Autowired 、@Scope、@Controller、@Service、@Repository、@Component、@RequiresPermission等等.
1 @Controller 2 @RequestMapping("/appointments") 3 public class AppointmentsController { 4 5 private AppointmentBook appointmentBook; 6 7 @Autowired 8 public AppointmentsController(AppointmentBook appointmentBook) { 9 this.appointmentBook = appointmentBook; 10 } 11 12 @RequestMapping(method = RequestMethod.GET) 13 public Map<String, Appointment> get() { 14 return appointmentBook.getAppointmentsForToday(); 15 } 16 17 @RequestMapping(value="/{day}", method = RequestMethod.GET) 18 public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) { 19 return appointmentBook.getAppointmentsForDay(day); 20 } 21 22 @RequestMapping(value="/new", method = RequestMethod.GET) 23 public AppointmentForm getNewForm() { 24 return new AppointmentForm(); 25 } 26 27 @RequestMapping(method = RequestMethod.POST) 28 public String add(@Valid AppointmentForm appointment, BindingResult result) { 29 if (result.hasErrors()) { 30 return "appointments/new"; 31 } 32 appointmentBook.addAppointment(appointment); 33 return "redirect:/appointments"; 34 } 35 }
example B)
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); model.addAttribute("owner", owner); return "displayOwner"; }
example C)
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}") public void handle(@PathVariable String version, @PathVariable String extension) { // ... }
@Controller @RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json") public void addPet(@RequestBody Pet pet, Model model) { // implementation omitted }
@Controller @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json") @ResponseBody public Pet getPet(@PathVariable String petId, Model model) { // implementation omitted }
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
http://www.ifeng.com/
”的请求;@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
application/x-www-form-urlencoded
编码的内容,提交方式GET、POST;示例代码:
@Controller @RequestMapping("/pets") @SessionAttributes("pet") public class EditPetForm { @RequestMapping(method = RequestMethod.GET) public String setupForm(@RequestParam("petId") int petId, ModelMap model) { Pet pet = this.clinic.loadPet(petId); model.addAttribute("pet", pet); return "petForm"; }
<?xml version="1.0" encoding="UTF-8" ?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <context:component-scan base-package=”com.eric.spring”> </beans>
@Service public class VentorServiceImpl implements iVentorService{ }
@Repository
public class VentorDaoImpl implements iVentorDao { }
getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“aaaaa”)这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”)@Scope(“prototype”)来改变。
可以使用以下方式指定初始化方法和销毁方法(方法名任意):
@PostConstruct
public void init() { } @PreDestroy
public void destory() { }
例如: @RequiresPermissions({"file:read", "write:aFile.txt"} )
void someMethod();
原文:http://www.cnblogs.com/myinspire/p/7273141.html