This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Not able to test AOP using AspectJ

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
    Bookmark Topic Watch Topic
  • New Topic