[英]How to set annotation property/attribute programmatically?
So I need to set the timeout parameter for @Transactional annotation. 所以我需要为@Transactional注释设置timeout参数。 This property will come from a property file which I'm not able to do since I'm encountering "The value for annotation attribute Transactional.timeout must be a constant expression". 这个属性将来自一个属性文件,我无法做到,因为我遇到“注释属性的值,Transactional.timeout必须是一个常量表达式”。 Something like this 像这样的东西
@Value("${mytimeout}")
private int myTimeout;
@Transactional(timeout=myTimeout)
public void myMethod(){
}
The only time the timeout attribute can be set by a variable is when a variable is final. 变量设置的唯一时间是变量是最终的。
So, I was thinking if it is possible to set the timeout property programmatically while using the @Transaction annotation. 所以,我在想是否可以在使用@Transaction批注时以编程方式设置超时属性。 Or any other way I can set this attribute Thanks! 或者我可以设置此属性的任何其他方式谢谢!
If you need the same timeout for all transactions, you can configure it as defaultTimeout
in your transaction manager 如果所有事务都需要相同的超时,则可以在事务管理器中将其配置为defaultTimeout
Otherwise, you may try to play with custom AnnotationTransactionAttributeSource
and TransactionAnnotationParser
, though you'll need to replace <tx:annotation-drivern>
with manual definition of corresponding beans in order to configure a custom attribute source. 否则,您可以尝试使用自定义AnnotationTransactionAttributeSource
和TransactionAnnotationParser
,但是您需要将<tx:annotation-drivern>
替换为相应bean的手动定义,以便配置自定义属性源。
Then you can create a custom annotation and make TransactionAnnotationParser
generate TransactionDefinition
with custom timeout when it sees your annotation. 然后,您可以创建自定义注释,并使TransactionAnnotationParser
在看到注释时生成具有自定义超时的TransactionDefinition
。
Otherwise, the easiest way to solve this problem is to give up using @Transactional
and use TransactionTemplate
instead. 否则,解决此问题的最简单方法是放弃使用@Transactional
并改为使用TransactionTemplate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.