This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring aspects, stumped... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring aspects, stumped..." Watch "Spring aspects, stumped..." New topic
Author

Spring aspects, stumped...

Michael Jenkins
Greenhorn

Joined: Jun 23, 2009
Posts: 5
I used Spring a few years ago, loved it... moved to a project that didn't use it, and now I've come back.

I'm trying to implement an advisor using some of the new features in Spring 2.5, but I can't get it to work, I'm completely stumped; I've searched online, I've searched these forums, I've consulted books.

This is a recreation of my attempt with some very simple code, and I hope that someone can please point out to me what I'm doing wrong.

This is the behavior I'm seeing: the TargetImpl.execute() method executes (the sys.out prints to the screen), but there is no evidence that the interceptor is ever loaded, and it certainly doesn't execute (its sys.out is not seen anywhere, debugger never shows a reference to it in scope, never passes through the code).

First, the "program":


Now, the "target":


the "impl":


now, the advisor:



Finally, the Spring config, notice that I've attempted to define the pointcut and advice using two different methods (annotation and explicit configuation in config file, commented out), but neither worked.



Some log4j output, snipped a bit to avoid spam:



What I notice about this output is that there is no mention of aspects, advisors, pointcuts, joinpoints, etc, as I would expect from some of the other "problem" threads I've seen. This output was produced with trace as the logging level for all classes. Is there something else I need to do to somehow enable that logging?

Environment data:
Linux, Ubuntu 64-bit, v9.04
Java 1.6.0_12-b04, 64-bit
Spring 2.5.6.SEC01


Thanks for reading through the email, any advice (yes, a pun, I know ) is appreciated...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What libraries are on your classpath? I don't see anything immediately obvious.
Michael Jenkins
Greenhorn

Joined: Jun 23, 2009
Posts: 5
Thanks for the question, David.

I'm using all 145 jars from the spring-with-dependencies distribution... it's overkill, but I wanted to make sure I wasn't leaving something out.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Good lord, man.

I'd actually caution *against* using every single jar since including a jar can sometimes alter application behavior--a time when Maven (or similar) can be very handy.

If I have a chance I'll try your sample; I've never used an aspect as a @Component, but you say it's broken w/ the XML config as well?
Michael Jenkins
Greenhorn

Joined: Jun 23, 2009
Posts: 5
Yeah, overkill... I tried it with the "essentials", then tried everything, same behavior. To determine the essentials, I started with spring.jar and added jars until things built/ran without exception.

Correct, it doesn't work with xml, either. I can delete all annotations and uncomment the XML in the spring config to produce the same behavior.

It seems that the aspects aren't being "discovered" somehow, only because I'm not seeing anything in the logs. It's my understanding that the <aop:aspectj-autoproxy /> should enable that, but I don't know how to verify it.

I'll remove the unused .jars. Is there a list that I should include, or is the "add jars until it doesn't complain" practice the right way to get there?
Michael Jenkins
Greenhorn

Joined: Jun 23, 2009
Posts: 5
Following up...

This was supposed to be a quick/dirty attempt to get advice working, so I wasn't picky about jars... but you had a point, David, so I wanted to make sure jars weren't the source of the problem.

I removed everything except for the spring-focused jars (spring.jar, spring-aspect.jar, etc.) from the classpath, then added things back until the app ran; this required me to add commons-logging, log4j.jar, aspectjrt.jar, and aspectweaver.jar.

Unfortunately, nothing changed, except for number of files that get copied from the 'lib' directory to the 'dist' directory when I build.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, wait--my bad; spaced this one. Read here.

In a nutshell there's bean post-processing that must take place for this kind of stuff to work; you should use a ClassPathXmlApplicationContext it works or a FileSystemXmlApplicationContext it also works.
Michael Jenkins
Greenhorn

Joined: Jun 23, 2009
Posts: 5
doh! That was it! Thanks, David!

I remember reading about the difference a few years ago and thinking "Somehow, this will bite me in the future."


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I hate those ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring aspects, stumped...
 
Similar Threads
Problem using EclipseLink -> org.springframework.dao.InvalidDataAccessApiUsageException
Transaction failure crashes program
"Application has unexpectedly stopped error" when adding onClickListener
How to have multiple public classes in one file for spring?
How to get aroun LazyInitialization exceptions using Spring HibernateDaoSupport?