Thursday, July 26, 2007

Declarative transaction management with Spring 2 and AspectJ

I'm playing with Hibernate, Spring 2 and AOP. Configuring this stuff were some kind of challenge for me. But now it woks. I faced wery interesting exeption during configuration. Since I'm using Maven 2, Spring + Hibernate dependency looks like this:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.5</version>
</dependency>

Exception is looks like this :

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 63 in XML document from class path resource [applicationContext.xml] is invalid;
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'aop:config'.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict, but no declaration can be found for element 'aop:config'.
Strange ! I have appropriate configuration for namespaces at my applicationContext.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
Config :
<aop:config>
<aop:advisor id="serviceTxAdvisor"
pointcut="execution(public *
com.company.service.impl.ServiceImpl.*(..))
"
advice-ref="txAdviceForService" />
</aop:config>

<tx:advice id="txAdviceForService"
transaction-manager
="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>

Solution suggested at Spring forum doesn't work for me. I had no success with Xerces. So, I decided to digg a little more. As a result I've found two missing dependencies that should make me happy:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>2.0.5</version>
</dependency>

Weird exception vanished. I'm very happy now.

BTW, even you use spring dependency, you should add spring-aspects dependency as well to be able to use aop:config.

4 comments:

Craig Walls said...

It looks like your error was similar to mine, but the problem was quite different. In your case, it couldn't even the AOP schema because that namespace is defined in spring-aop.jar. Since you didn't have spring-aop.jar in your classpath, you received that error.

In my case, I had all of the necessary JARs in my classpath, but Eclipse was complaining because it was looking at http://www.springmodules.org for the XSD file. Since the XSD isn't at http://www.springmodules.org, I had to add it to Eclipse's XML catalog to make it happy.

Stas Ostapenko said...

Hi Craig !

Thanks you very much for your comment ! Finally I'm understand why I've got such exception. Really, shema is in spring-aop-2.0.5.jar file (aop\config\spring-aop-2.0.xsd).

By the way, line "cvc-complex-type.2.4.c" looks very interesting, at least uncommon ;)

Eric said...

Craig, I don't understand your explanation. His file references the aop schema at http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
I can enter this into by browser and it resolved fine, I can read the schema file. So why is this a problem, and why would it look in the spring-aop.jar instead?

Stas Ostapenko said...

Hi Eric !

Interesting question. I haven't time to digg in this direction right now. I'm just happy that all this stuff works. But I want to know why it is working in such way and not some another.