博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring -- 自定义注解
阅读量:4631 次
发布时间:2019-06-09

本文共 2963 字,大约阅读时间需要 9 分钟。

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

package com.sysware.cloud.dts.annotation;import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD})@Inherited@Documentedpublic @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;@Servicepublic class PointServiceImpl extends BaseServiceImpl
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统一处理,参考我的博客

 

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

你可能感兴趣的文章
算法导论 6-2 代码实现 C++
查看>>
Floyd-傻子也能看懂的弗洛伊德算法(转)
查看>>
python3 常见错误(一)
查看>>
php中怎么实现后台执行?
查看>>
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
查看>>
敏捷开发方法综述
查看>>
C语言中库函数strstr的实现
查看>>
python-希尔排序
查看>>
Vue中ref的使用要点
查看>>
python 日志
查看>>
Qt 学习之路 2(84):Repeater
查看>>
form表单文件上传
查看>>
购物车组件开发
查看>>
Python 列表生成器
查看>>
Linux Shell编程从初学到精通
查看>>
Linux and the Device Tree
查看>>
「luogu2414」[AH2017/HNOI2017]礼物
查看>>
C# 控件命名规范
查看>>
求数组中两个子数组最大和
查看>>
javascript实现下雪效果
查看>>