views:

744

answers:

3

What I want to do:

  • I want to use the @Configured annotation with Spring. It requires AspectJ to be enabled. I thought that using the AJDT plugin for compile time weaving would solve this problem. Before installing the plug in the dependencies which were supposed to be injected into my @Configured object remained null.

What I have done:

  • Installed the AJDT: AspectJ Development Tools plug in for Eclipse 3.4.
  • Right clicked on my web project and converted it into a AspectJ project.
  • Enabled compile time weaving.

What doesn't work:

  • When I start the Tomcat 6 server now, I get an exception*.

Other information:

  • I haven't configured anything in the AspectJ Build and AspectJ Compiler parts of the project properties.
  • JDT Weaving under Preferences says weaving is enabled.
  • I still have Java build path and Java Compiler under project properties. And they look like I previously configured them (while the above two new entries are not configured).
  • The icon of my @Configured object file looks like any other file (i.e. no indication of any aspect or such, which I think there should be). The file name is MailNotification.java (and not .aj), but I guess it should still work as I'm using a Spring annotation for AspectJ?
  • I haven't found any tutorial or similar which teaches: How to turn a Spring web application project into an AspectJ project and weave aspects into the files using the AJDT plugin, all within Eclipse 3.4. If there is anything like that out there I would be very interested in knowing about it.

What I would like to know:

  • Where to go from here? I just want to use the @Configured annotation of Spring. I'm also using @Transactional which I think also needs AspectJ.
  • If it is possible I would like to study AspectJ as little as possible as long as my needs are met. The subject seems interesting, but huge, all I want to do is use the above two mentioned Spring annotations.

*** Exception when Tomcat 6 is started:

Caused by: java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:82)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 41 more
A: 

Have you added spring-aspects.jar to your aspect path for the project?

In the project properties, under 'AspectJ Build' -> 'Aspect Path' try adding spring-aspects.jar and clean building the project.

Sorry you might have already done this - but you didn't mention it.

Rob Beardow
I haven't added it or done any specific configuration really (like stated above). I can't seem to find any good documentation for the AJDT plugin so I don't really know what I need to do. :-/
DeletedAccount
A: 

It looks like the compile time weaving isn't working. Try adding the below lines to your applicationcontext.xml

<context:load-time-weaver />
<context:spring-configured/>

You'll probably want to add the following xsd to the xml file also

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

For details see here:

http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-aj-ltw

Pablojim
A: 

You can use @Transactional without AspectJ. Your configuration file should contain something like following to make it work:

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-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/util 
  http://www.springframework.org/schema/util/spring-util-2.5.xsd"
  >
<tx:annotation-driven/>

tells spring to look for @transactional annotations when creating instances of configured beans. On finding such annotation, spring returns a dynamic proxy of the bean to the application code. This dynamic proxy ensures that whenever the annotated methods are called, spring is able to intercept it to provide intended transactional behavior. But the proxy-based AOP mandates that you code against interfaces and not concrete classes.

Tahir Akhtar