Spring - BeanCreationException or CGLIB2 when using @Transactional(readOnly = true)

February 18, 2013

Problem:

If the instruction @Transactional(readOnly = true) is causing the bellow error(s):

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx' defined in class path resource \[xxx/xxx.xml\]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

Solution:

Add the cglib jar or maven dependency to your project:

<dependency>  
   <groupid>cglib</groupid>  
   <artifactid>cglib</artifactid>  
   <version>2.2</version>  
</dependency>