Do Spring class completion service?

Can I make spring service classes final? Is there any harm? No one is going to expand the class. Is there a problem?

public final class MyService {
   // Depedencies go here.
}
+5
source share
2 answers

Do not do them final. If you use any AOP (including transaction support) for specific classes, spring will use CGLIB to dynamically extend your class to make proxies. And for CGLIB to work, your classes should not be final. Otherwise, an exception will be thrown.

+14
source

Spring will create a dynamic JDK proxy, not a CGLIB proxy, if the following is true:

  • aop: config - , false
  • (, tx: ) - , false

, . ( package-private , , Spring ).

Spring - CGLIB. ( , @Repository, PersistenceExceptionPostBeanProcessor), bean . , CBLIB. (: CGLIB , ).

? , ( ) bean, . JDK. , , . , . DAO, - ? DAO , - DAO . ( ) , .

( ), , . (. ). .

+4

All Articles