2013-01-04 43 views
6

我創建自己的自定義快捷方式的註釋,如Spring Documentation描述: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

不,這是行不通的,如果你想將必須對您的自定義註釋本身這種方式設置附加屬性:

@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 { 
} 
+0

是的,我認爲這樣的場景中的,但後來我最終會與MANY註釋 - 對於每個明智的屬性組合;) – Laimoncijus

+1

是,我這麼認爲,這就是爲什麼我把它作爲一個糟糕的解決方案:-)。除非使用'@ Transactional'本身,否則沒有看到任何其他選項,如果您有一組固定的註釋屬性,那麼自定義的構造型註釋將會有所幫助。 –