2

Consider this scenario for a Java application with Spring:

public interface FooDao {
    @Transactional
    void save(Foo foo);
}

public interface SecureFooDao extends FooDao {
    @Secured({Role.ADMIN})
    void save(Foo foo);
}

My question is this; will calling save on a SecureFooDao interface start a transaction, or will it ignore the overriden methods annotations?

crunchdog
  • 13,078
  • 3
  • 23
  • 19

1 Answers1

3

From Spring reference 10.5.6 Using @Transactional

Tip

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.

So even not overriden it will only work if you use Spring-Aop-Proxies (which I can not recommend), but not for AspectJ or CGILib Proxies!

But I do not expect that this work for an method that is overriden in an Interface, even not for Spring-Aop-Proxies.

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383