spring常用注解(未完待续)

来源:互联网 发布:网络黄金egd还有希望吗 编辑:程序博客网 时间:2024/05/22 17:18

1.@Controller@RestController@RequestMapping注解。

    @Controller:修饰class,用来创建处理http请求的对象

 @RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller

               不需要再配置@ResponseBody,默认返回json格式。

 @RequestMapping:配置url映射


2.@PostConstruct和@PreDestroy

       这两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作。

@AutowiredSchedulerscheduler;/** * 项目启动时,初始化定时器 * @author lyd * @date 2017年10月14日 */@PostConstructpublic void init() throws Exception {List<ScheduleJobEntity> scheduleJobList = scheduleJobDao.queryList(new ScheduleJobQuery());for (ScheduleJobEntity scheduleJob : scheduleJobList) {CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());//如果不存在,则创建if(cronTrigger == null){ScheduleUtils.createScheduleJob(scheduler, scheduleJob);} else {ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);}}}