gpt4 book ai didi

java - Spring 自定义注解 : how to inherit attributes?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:25:45 25 4
gpt4 key购买 nike

我正在创建自己的自定义快捷方式注释,如 Spring Documentation 中所述:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = "Custom", readOnly = true)
public @interface CustomTransactional {
}

有没有可能,通过我的自定义注释,我还可以设置任何其他属性,这些属性在 @Transactional 中可用?我希望能够使用我的注释,例如,像这样:

@CustomTransactional(propagation = Propagation.REQUIRED)
public class MyClass {

}

最佳答案

不,如果您想要必须以这种方式在自定义注释本身上设置的其他属性,那将行不通:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = "Custom", readOnly = true, propagation = Propagation.REQUIRED)
public @interface CustomTransactional {
}

一个解决方案(不好的 :-) )可能是使用您在场景中看到的基本案例集定义多个注释:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = "Custom", readOnly = true, propagation = Propagation.REQUIRED)
public @interface CustomTransactionalWithRequired {
}

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value = "Custom", readOnly = true, propagation = Propagation.SUPPORTED)
public @interface CustomTransactionalWithSupported {
}

关于java - Spring 自定义注解 : how to inherit attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14154498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com