【问题标题】:Spring @Transactional v Spring Security @Secured inconsistent behaviourSpring @Transactional v Spring Security @Secured 不一致的行为
【发布时间】:2012-01-16 12:11:55
【问题描述】:

Spring 文档建议将 @Transactional 注释放在具体的类/方法上而不是接口上。这个原因在 Stack Overflow 上已经讲过很多次了,例如:

Where should I put @Transactional annotation: at an interface definition or at an implementing class?

Spring Security @Secured 行为不同;大多数文档显示将注释放在界面上。事实上,无论您是注释接口还是具体类,无论您使用 JDK 还是 CGLib 代理,它似乎都可以工作。

这似乎是一个更好的解决方案。那么为什么不一致呢?对上述问题的一个答案表明会影响性能……但性能肯定对安全性同样重要吗?!

@Secured 解决方案如何处理菱形继承问题(类实现 2 个接口都具有不同的 @Secured 相同方法)?

【问题讨论】:

    标签: spring annotations spring-security spring-aop spring-transactions


    【解决方案1】:

    当同时使用 JDK 代理和 CGLib 时,您最终会得到 TransactionInterceptor 对应 @TransactionalMethodSecurityInterceptor 对应 @Secured

    但是这两个MethodInterceptors 使用不同的机制在给定的MethodInvocation 上查找注释。

    @Secured 注解是由SecuredAnnotationSecurityMetadataSource 使用AnnotationUtils.findAnnotation(Method method, Class<A> annotationType) 方法发现的,而@Transactional 是由AnnotationTransactionAttributeSourceSpringTransactionAnnotationParser 的帮助下发现的。

    看起来 AnnotationUtils 有更高级的机制来查找注解、搜索接口和声明类层次结构的方法。

    您可以创建自己的使用 AnnotationUtils 的 TransactionAnnotationParser,这应该启用与 @Transactional 相同的功能。

    AnnotationUtils 返回第一个找到的注解,这就是菱形继承的处理方式。

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 2014-03-09
      • 2021-09-27
      • 2011-01-23
      • 2013-08-02
      • 2016-05-25
      • 1970-01-01
      • 2021-01-26
      • 2012-09-26
      相关资源
      最近更新 更多