spring boot常用注解小计
@Async
需要执行异步方法时,在方法上加上@Async之后,底层使用多线程技术 。启动类上需要加上 @EnableAsync
注意:异步执行方法,不能与引用方法同在一个类里
@Transactional
org.springframework.transaction.annotation包 ,不是来自javax.transaction 。而且@Transactional不仅可以注解在方法上,也可以注解在类上。当注解在类上的时候意味着此类的所有public方法都是开启事务的。如果类级别和方法级别同时使用了@Transactional注解,则使用在类级别的注解会重载方法级别的注解。
/**
* 添加一个事务
*/
@Transactional(rollbackFor = Exception.class)
public void tranTest() {
/**
* 事务中重新开启新的事务
*/
((TestService) AopContext.currentProxy()).newTranTest();
} /**
* 另起一个新事务
*/
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void newTranTest(){ }
@EnableTransactionManagement
注意:Springboot提供了一个@EnableTransactionManagement注解在配置类上来开启声明式事务的支持。注解@EnableTransactionManagement是默认打开的,想要关闭事务管理,想要在程序入口将这个注解改为false
@Scheduled(fixedDelay = 时间间隔 )
@Scheduled(fixedRate = 时间间隔 )
@Scheduled(cron = cron表达式)
启动
要在主方法上加上 @EnableScheduling
类中添加@Slf4j 注解即可。使用是直接输入log全局变量
Lombok的其他用法
@Data 标签,生成getter/setter toString()等方法
@NonNull : 让你不在担忧并且爱上NullPointerException
@CleanUp : 自动资源管理:不用再在finally中添加资源的close方法
@Setter/@Getter : 自动生成set和get方法
@ToString : 自动生成toString方法
@EqualsAndHashcode : 从对象的字段中生成hashCode和equals的实现
@NoArgsConstructor/@RequiredArgsConstructor/@AllArgsConstructor
自动生成构造方法
@Data : 自动生成set/get方法,toString方法,equals方法,hashCode方法,不带参数的构造方法
@Value : 用于注解final类
@Builder : 产生复杂的构建器api类
@SneakyThrows : 异常处理(谨慎使用)
@Synchronized : 同步方法安全的转化
@Getter(lazy=true) :
@Log : 支持各种logger对象,使用时用对应的注解,如:@Log4
@EnableCaching
在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者: Generic , JCache (JSR-107), EhCache 2.x ,Hazelcast , Infinispan ,Redis ,Guava , Simple
spring boot常用注解小计的更多相关文章
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- Spring Boot 常用注解汇总
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Spring Boot常用注解
SpringBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enable ...
- spring boot常用注解使用小结
1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Sprin ...
- spring boot 常用注解
@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,spring M ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- spring boot——常用注解
@SpringBootApplication:申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 ...
- (一)Spring Boot 常用注解
文章目录 一.注解(annotations)列表 二.注解(annotations)详解 三.JPA注解 四.SpringMVC相关注解 五.全局异常处理 @ExceptionHandler(Exce ...
- Spring Boot常用注解和原理整理
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
随机推荐
- python MySQL执行SQL查询结果返回字典
写自动化测试的时候我希望执行数据库前置任务,把数据库查询的结果作为请求的参数,但是正常返回结果为列表嵌套里面,这样就会影响到关键字准确的获取,特别的受限于SQL的查询字段的的顺序,所以希望返回的单条数 ...
- 定时任务模块 schedule
# coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...
- 通用唯一识别码UUID
UUID 概念:UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,目前最广泛应用的UUID,是微软公司的全局唯一标识符(GUID),而其他重要的应用,则 ...
- Ubuntu16.04下安装nginx+mysql+php+redis
一.redis简介 Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记 ...
- MySQL死锁分析一例
Tomcat日志报死锁错误,show innodb status获取死锁信息: ------------------------ LATEST DETECTED DEADLOCK ---------- ...
- mac brew install error
Error: No available formula with the name "pygame" ==> Searching for a previously delet ...
- 继承LinearLayout自定义左侧菜单
public class LeftMenuView extends LinearLayout { LinkedHashMap<Integer,String> map=new LinkedH ...
- three.js中的文字
1.三维文字 三维字体文字,使用的是FontLoader,字体文件通过来facetype.js生成 addCityText: function () { var self = this; var ci ...
- 配置RIPng(PT)
一:拓扑图 二:配置过程 1:首先为pc0:pc1: pc2 配置IPv6地址(注意标明前缀),可以手动配置也可以自动获取. 手动配置 自动获取 2:给路由器配置RIPng协议 全局开启RIPng协议 ...
- 关于Python的Mixin模式
转自:http://www.bjhee.com/python-mixin.html 像C或C++这类语言都支持多重继承,一个子类可以有多个父类,这样的设计常被人诟病.因为继承应该是个”is-a”关系. ...