我们直接通过代码解释自定义注解的使用及各个含义

package com.sysware.cloud.dts.annotation;import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Inherited
@Documented
public @interface DtTransactional {/** Whether need to rollback*/public boolean includeLocalTransaction() default true;public boolean confirmMethodExist() default false;/** Allow [confirmMethod] is null if [confirmMethodExist] is false*/public String confirmMethod() default "";public String cancelMethod() default "";}

说明1:@Target、@Retention、@Inherited、@Documented为元注解(meta-annotation),它们是负责注解其他注解的。

1:Target注解 :指明注解支持的使用范围,取值可以参考ElementType :

  • ElementType.TYPE //类、接口、枚举
  • ElementType.FIELD //属性
  • ElementType.METHOD //方法
  • ElementType.PARAMETER //参数
  • ElementType.CONSTRUCTOR //构造器
  • ElementType.LOCAL_VARIABLE //局部变量
  • ElementType.ANNOTATION_TYPE //注解
  • ElementType.PACKAGE //包

2:Retention注解 :指明注解保留的时间长短,取值参考枚举RetentionPolicy :

  • SOURCE //源文件中保留
  • CLASS //class编译时保留
  • RUNTIME //运行时保留

3:Inherited 注解:指明该注解类型被自动继承。如果一个annotation注解被@Inherited修饰,那么该注解作用于的类的子类也会使用该annotation注解。

4:Document 注解:指明拥有这个注解的元素可以被javadoc此类的工具文档话。

说明2:在自定义注解中定义的属性有 default ,使用该注解的时候可以不填写该属性,否则该属性为必填,不填会提示错误。

上面自定义注解就定义好了,接下来就可以在Target指定的范围内使用该注解了,比如上面定义的注解是使用在 METHOD 上的,可以这样使用

package com.sysware.cloud.dts.service.impl;import com.sysware.cloud.commons.util.idwork.LongIdGenerator;
import com.sysware.cloud.dts.annotation.DtTransactional;
import com.sysware.cloud.dts.entity.PointEntity;
import com.sysware.cloud.dts.repository.PointRepository;
import com.sysware.cloud.dts.service.PointService;
import com.sysware.cloud.po.CriteriaQuery;
import com.sysware.cloud.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;
import java.util.concurrent.CancellationException;

@Service
public class PointServiceImpl extends BaseServiceImpl<PointEntity,PointRepository,Long> implements PointService {@Override@Transactional@DtTransactional(cancelMethod = "cancel")public PointEntity add(PointEntity entity) {//根据用户ID查询是否积分记录,没有则生成积分,有的话,则修改添加积分PointEntity pointEntity = super.repository.findPointEntitiesByUserId(entity.getUserId());if(pointEntity==null){entity.setId((LongIdGenerator.getId()));return super.repository.save(entity);}else {pointEntity.setPoint(pointEntity.getPoint()+entity.getPoint());return super.repository.save(pointEntity);}}public PointEntity cancel(PointEntity entity) {PointEntity pointEntity = super.repository.findPointEntitiesByUserId(entity.getUserId());if (pointEntity != null) {pointEntity.setPoint(pointEntity.getPoint() - entity.getPoint());return super.repository.save(pointEntity);}return null;}
}

最后就是如何解析自定义注解了, 解析过程可以使用AOP统一处理,参考我的博客spring --解析自定义注解SpringAOP(配合@Aspect

转载于:https://www.cnblogs.com/wenq001/p/9144278.html

spring -- 自定义注解相关推荐

  1. Spring自定义注解

    转载自:Spring自定义注解 前言 自定义注解在开发中是一把利器,经常会被使用到.在上一篇文章中有提到了自定义校验注解的用法. 然而最近接到这样一个需求,主要是针对某些接口的返回数据需要进行一个加密 ...

  2. Spring 自定义注解使用案例 首先创建一个注解@interface

    在Spring中使用自定义注解的本质就是使用Spring 的 AOP编程. 首先创建一个注解@interface import org.springframework.data.mongodb.cor ...

  3. Spring自定义注解简单使用四步走

    在实际开发中,很多时刻我们会有记录请求日志,定时任务日志等需求,在每个方法中都编写相同的代码去记录日志显然是不合理的. Spring已经为我们提供了面向切面编程的思想,不妨简单的使用下自定义注解. 简 ...

  4. 深入Spring:自定义注解加载和使用

    转自:https://blog.csdn.net/z69183787/article/details/53784845 前言 在工作中经常使用Spring的相关框架,免不了去看一下Spring的实现方 ...

  5. Spring 自定义注解,配置简单日志注解

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  6. java 扫描自定义注解_利用spring 自定义注解扫描 找出使用自定义注解的类

    我们常常有扫描项目里带有指定注解的class, 下面是利用spring扫描自定义注解的方法, 还是比较灵活的 我这里将扫描到的class放到map, 你可以放到其他地方,以便后期使用 import l ...

  7. Spring自定义注解驱动开发使用及源码分析

    目录 前言 注解驱动开发使用 需求 代码实现 测试效果 源码分析 BeanDefinitionRegistryPostProcessor接口 解析BeanDefinition 处理Bean上配置的注解 ...

  8. Spring自定义注解实现事务

    首先spring中事务有两种方式:编程事务 .声明事务 编程事务: 获取项目中事务管理器 DataSourceTransactionManager 采用事务管理提交回滚操作 @Component pu ...

  9. 5.概念(maven,ssm,springMvc,spring,自定义注解,二级缓存,范式,事务,mysql,线程池,map,hashmap,redis,饿汉,懒汉)

    maven是啥: 1.Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架. 创建-导入jar报–编写配置文件-实现业务功能-测试-发布上线. 2.开发团队可以自动完 ...

最新文章

  1. textarea 自适应窗口|IE、firefox 显示同样的效果
  2. 为预防软件质缺陷应该做点儿什么
  3. linux制作ext2磁盘镜像,linux--创建镜像挂载
  4. stmcubemx 脉冲计数_STM32CubeMX:ETR外部脉冲计数器
  5. 即将上线的Kafka 集群(用CM部署的)无法使用“--bootstrap-server”进行消费,怎么破?...
  6. 回溯法 —— 判断子集和问题是否存在解
  7. ORA-01031:insufficient privileges
  8. 2014华为机试西安地区B组试题
  9. 怎么通过Excel导入sqlserver数据库(无废话)
  10. matlab如何插入“埃”这个符号
  11. PHPStudy设置局域网访问
  12. 如何写好一个数据分析报告,需要注意哪些?
  13. 疲劳检测——眨眼检测
  14. 跨境电商未来将合规化发展——扬帆牧哲
  15. 逆水寒服务器维护公告,《逆水寒》2018年11月29日更新公告
  16. 从静态检查工具谈代码编程规范
  17. VBA遍历文件夹下的所有文件
  18. C#System.NullReferenceException:未将对象引用设置到对象的实例
  19. 香港电影中的演员:F字头(持续更新中)
  20. 浅谈C++的函数重载

热门文章

  1. Apache的工作原理
  2. Compiere去掉启动时的下面显示的进度条
  3. gi克隆github文件_如何构建GitHub文件搜索功能的克隆
  4. 夏天和空调_您可以在今年夏天开始学习650项免费的在线编程和计算机科学课程...
  5. 数据结构中等号表示什么_通过分析2016年最重要的252个中等故事我学到了什么...
  6. 数据结构的简要介绍:图形如何工作
  7. java代码操作git_JGit--实现Git命令操作的Java API
  8. react 用html插件,React配置过程中用到的插件汇总
  9. java培训分享:学习Java需要什么软件
  10. Bootstrap3基础 btn-group-vertical 按钮组(横着、竖着排列)