This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Not able to test AOP using AspectJ

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am trying to use the auto proxying feature of Spring AOP. There are two ways to do it

1. using the basic auto proxying
2. using @AspectJ Annotation.

I tried running sample code for both, and could not see the auto prozying feature for both. But I am concentrating only on the way 2.

Here are the relevant code fragments:

The XML, the AOP part of it:



The Java Classes:







I am using a plain java application that gets bean and uses them as:


The code for the Performer Interface:
package com.springinaction.springidol;



When I run the application, I get to see only tthe sys outs of the Instrumentalist class and the Saxophone class. This shows that the functionality of the aspect is not associated with the bean. Am I missing something?

The code for Instrumentalist class

package com.springinaction.springidol;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;



public class Instrumentalist implements Performer, InitializingBean, DisposableBean {
private String song;
private Instrument instrument;
private Stage stage;

public Instrumentalist() {}

public void perform() throws Exception {//PerformanceException
System.out.print("Playing " + song + " : ");
instrument.play();
}

public void setSong(String song) {
this.song = song;
}

public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}

public Stage getStage() {
return stage;
}

public void setStage(Stage stage) {
this.stage = stage;
}

public void tuneInstrument() {
instrument.tune();
}
public void cleanInstrument() {
instrument.clean();
}

public void afterPropertiesSet() throws Exception {
instrument.tune();

}

public void destroy() throws Exception {
instrument.clean();
}
}

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I understand, any bean defined in the application context with the @AspectJ annotation will be detected by Spring. So unless the AudienceAspect is loaded, I don't think Spring will know about it. Please try the following :

1. Add the @AspectJ annotated bean in the context file


2. Use the ApplicationContext, so that your beans will be loaded by Spring

[ February 29, 2008: Message edited by: Christophe Verre ]
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks a lot, the code is working as desired.

I agree with you on point1. The advice bean was not declared in my xml file.
I declared it.

But I did not get point 2. Why did XMLBeanFactory failed to work? Please explain.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A BeanFactory instantiates beans only when needed, so the advice will not be instantiated unless you explicitly load it, or reference it in an other loaded bean. On the contrary, an ApplicationContext instantiates all beans, unless you explicitly tell Spring not to.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Christophe.

I think I have tried all the four ways of AOP in Spring. But as said, the aop:config seems most convinient and easy to use.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic