Posts mit dem Label AOP werden angezeigt. Alle Posts anzeigen
Posts mit dem Label AOP werden angezeigt. Alle Posts anzeigen

Donnerstag, 13. Mai 2010

Using Spring AOP with Grails Services

If you try to work with Spring AOP and service classes in a Grails application, you will probably end up with an exception like:

Error executing script TestApp: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'testService':
Invocation of init method failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy11]: Common causes of this problem include using a final class or a non-visible class;
nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy11


To solve this problem you have to add the following line to your resources.xml (in grails-app/conf/spring):
<aop:aspectj-autoproxy proxy-target-class="true"/>

Your final resources.xml will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 <aop:aspectj-autoproxy proxy-target-class="true"/> 
 
</beans>


This was tested with Grails 1.2.2 and Grails 1.3.0.