Spring常用注解

Spring 核心注解

@Autowired
@Qualifier
@Configuration
@Required       在bean的set方法上面使用,表示这个属性是必须要注入的,负责抛出   BeanInitialzationException
@ComponentScan 和@Configuration一起使用 ,指定Spring扫描注解的包,默认扫描配置类所在的包
@Lazy  延迟初始化bean,在第一次调用的时候初始化  :  使用在组件类上面,也可以使用在Configuration注解的类上面,表示所有的被@Bean注解的方法都会延迟初始化
@Value 在字段,构造器参数和方法参数上面使用,可以指定属性取值的表达式,支持 #{} 使用SpringEL取值,也支持使用 ${} 将属性来源中 (  Properties文件,yml文件,  本地环境变量,系统属性 )注入到Bean属性中)    此注解的值注入发生在AutowiredAnnotationBeanPostProcessor类中

Spring MVC 和REST注解

@Controller
@RestController
@ControllerAdvice
@RestControllerAdvice
@RequestMapping
@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
@RequestAttribute
@RequestBody
@ResponseBody
@ResponseStatus
@PathVariable
@RequestParam
@CookieValue    使用在@RequestMapping声明的方法参数上面,可以把Http Cook中相应的名称绑定在上面,   name = "JSESSIONID"
@CrossOrigin    使用在类和方法上面,用来支持跨域请求  Spring4.2之后引入
@ExceptionHandler
@InitBinder     使用在方法上面,表示为当前编辑器注册一个属性编辑器
@RequestHeader  获取请求头
@SessionAttribute
@SessionAttributes    
@MatrixVariable
@RequestPart

数据访问

@Transactional

任务执行,调度

@Scheduled  定时调度,使用此注解的方法不能有返回值也不能接受参数   https://www.jianshu.com/p/1defb0f22ed1
        @Scheduled(fixedDelay = 1000)
        @Scheduled(fixedRate = 1000)
        fixedRate与fixedDelay的区别在于fixedRate不会等待是一个任务结束

@Async 异步方法: 可以接收参数,返回值类型必须是 Future
    使用注意:
        1、默认情况下(即@EnableAsync注解的mode=AdviceMode.PROXY),同一个类内部没有使用@Async注解修饰的方法调用@Async注解修饰的方法,是不会异步执行的,这点跟 @Transitional 注解类似,底层都是通过动态代理实现的。如果想实现类内部自调用也可以异步,则需要切换@EnableAsync注解的mode=AdviceMode.ASPECTJ,详见@EnableAsync注解。
        2、任意参数类型都是支持的,但是方法返回值必须是void或者Future类型。当使用Future时,你可以使用 实现了Future接口的ListenableFuture接口或者CompletableFuture类与异步任务做更好的交互。如果异步方法有返回值,没有使用Future<V>类型的话,调用方获取不到返回值。
 

格式化

@DateTimeFormat 日期格式化

        属性:

        前端必须输入yyyy-MM-dd格式的日期

@NumberFormat 数字格式化

         属性:

         style = NumberFormat.Style.CURRENCY前端输入¥100 后端解析为 100.00

SpringBoot

    @EnableAutoConfiguration
    @ComponentScan
    @SpringBootApplication

@CookieValue

@RequestMapping("/add_cookie")
	public String initBinderTest(HttpServletResponse response) {
		Cookie cookie = new Cookie("cookieTest", "cookieTest");
		return "add_cookie";
	}
	
	@RequestMapping("/cookie")
	public String cookeTest(@CookieValue("token") String token, @CookieValue("cookie_9") String cookie) {
		System.err.println("token: " + token);
		return "cookie";
	}

@InitBinder

当前Controller有效

@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));

	}

全局有效

@RestControllerAdvice
public class GlobalControllerAdvice {

	@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	}

}

测试

@RequestMapping("/initBinder")
	public String initBinderTest(@RequestParam("test1") String test1, @RequestParam("date") Date date) {
		System.err.println("test1 :" + test1);
		System.err.println("date :" + date);
		return "initBinderTest";
	}

@RequestHeader

@RequestMapping("/handlerTest")
	public String handlerTest(@RequestHeader Map<String, String> handler, @RequestHeader("test") String test,
			@CookieValue("cookieTest") String cookie) {
		System.err.println(test);
		System.err.println(cookie);
		for (Entry<String, String> element : handler.entrySet()) {
			System.err.println(element.getKey() + " : " + element.getValue());
		}
		return "handlerTest";
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值