前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何将只能作用一次的注解修改为作用多次

如何将只能作用一次的注解修改为作用多次

原创
作者头像
半月无霜
发布2024-07-19 14:19:39
610
发布2024-07-19 14:19:39
举报
文章被收录于专栏:半月无霜

如何将只能作用一次的注解修改为作用多次

一、介绍

当一个注解只能作用于一次类上,如何修改代码,使其能够作用于多次

就以下面的这个注解为例子

代码语言:javascript
复制
package com.banmoon.test.spv.annotation;
​
import com.banmoon.test.spv.listener.SystemPropertyTestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
​
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
​
/**
 * @author banmoon
 * @date 2024/07/16 15:16:05
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@TestExecutionListeners(listeners = SystemPropertyTestExecutionListener.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public @interface SystemPropeerty {
​
    /**
     * 系统变量key
     */
    String key();
​
    /**
     * 系统变量value
     */
    String value();
​
}

只能作用于类上,且只能一次

二、代码

思路,原本的注解只能作用一次,我们直接新写一个注解,将旧的注解当做一个数组进行,间接达到多次注解的效果

代码如下

代码语言:javascript
复制
package com.banmoon.test.spv.annotation;
​
import com.banmoon.test.spv.listener.SystemPropertyTestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
​
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
​
/**
 * @author banmoon
 * @date 2024/07/16 15:16:05
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@TestExecutionListeners(listeners = SystemPropertyTestExecutionListener.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public @interface SystemPropeerties {
​
    SystemPropeerty[] value();
​
}

相应的,我们的SystemPropertyTestExecutionListener.java需要改动,两个注解用的都是同一个

如此一来,我们需要做兼容

代码语言:javascript
复制
package com.banmoon.test.spv.listener;
​
import com.banmoon.test.spv.annotation.SystemPropeerties;
import com.banmoon.test.spv.annotation.SystemPropeerty;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;
​
import java.util.Arrays;
import java.util.Objects;
​
/**
 * @author banmoon
 * @date 2024/07/16 14:02:42
 */
public class SystemPropertyTestExecutionListener extends AbstractTestExecutionListener {
​
    @Override
    public void beforeTestClass(TestContext testContext) {
        SystemPropeerties systemPropeerties = testContext.getTestClass().getAnnotation(SystemPropeerties.class);
        if (Objects.nonNull(systemPropeerties)) {
            Arrays.stream(systemPropeerties.value()).forEach(item -> {
                String key = item.key();
                String value = item.value();
                System.setProperty(key, value);
            });
        }
        SystemPropeerty systemPropeerty = testContext.getTestClass().getAnnotation(SystemPropeerty.class);
        if (Objects.nonNull(systemPropeerty)) {
            String key = systemPropeerty.key();
            String value = systemPropeerty.value();
            System.setProperty(key, value);
        }
    }
}

三、最后

简单的来说,就是新写一个注解,里面有个原来注解数组的属性,再简单改造一下注解处理类,就能达到作用多次的效果了。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何将只能作用一次的注解修改为作用多次
    • 一、介绍
      • 二、代码
        • 三、最后
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档