7

Springのドキュメントで説明されているように、独自のカスタムショートカットアノテーションを作成しています。

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

カスタムアノテーションを使用して、で使用可能な他の属性を設定することもできます@Transactionalか?たとえば、次のように注釈を使用できるようにしたいと思います。

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

}
4

2 に答える 2

4

いいえ、カスタムアノテーション自体に次のように設定する必要がある追加の属性が必要な場合は、機能しません。

@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 {
}
于 2013-01-04T13:31:13.873 に答える
2

(少なくとも)Spring 4では、アノテーション内の要素を指定することでこれを行うことができます。例:

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

ソース: http: //docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-meta-annotations

于 2015-07-16T19:10:02.053 に答える