wood burning stoves*
The moose likes Spring and the fly likes AOP not getting invoked on my Target Bean Method --> Singer.perform() method 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 "AOP not getting invoked on my Target Bean Method --> Singer.perform() method" Watch "AOP not getting invoked on my Target Bean Method --> Singer.perform() method" New topic
Author

AOP not getting invoked on my Target Bean Method --> Singer.perform() method

Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 121

I am using POJO Spring AOP.

Singer bean is used as Target Bean to be applied AOP. Audience bean is used as Aspect.

Please let me know where is the problem. Source code listed below.

Performer.java -> Interface for target bean
================================================
package com.spring.springidol;
public interface Performer {
public void perform() throws PerformaceExcetion;
}

Singer.java -> Target bean whose perform method need to be AOPiesed
=================================================
package com.spring.springidol;
public class Singer implements Performer{
private String song = null;
public void setSong(String song){
this.song = song;
}
public void perform() throws PerformaceExcetion {
System.out.println("Singing: "+song);
}
}

Audience.java -> POJO Class to be used as Aspect for Singer Bean.
=============================================
package com.spring.springidol;
public class Audience {
public Audience(){
System.out.println("Audience initialized");
}
public void performance(){

}
public void takeSeats(){
System.out.println("Audience taking Seats");
}
public void switchOffPhone(){
System.out.println("Audience Swithing off phones");
}
public void askRefund(){
System.out.println("Audience asking for refund");
}
public void applause(){
System.out.println("CLAP!! CLAP!! CLAP!!");
}
}

TestAOPOfPerformer.java -> Test program to invoke Singer.perform() method
==================================================
package com.spring.springidol;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class TestAOPOfPerformer {
public static void main(String a[]) throws PerformaceExcetion{
Resource resource = new FileSystemResource("src/java/com/spring/springidol/aop.xml");

BeanFactory factory = new XmlBeanFactory(resource);
Performer performer = (Performer)factory.getBean("singer2");
performer.perform();
}
}

aop.xml -> Spring Configuration file to cofigure beans and aop-aspects.
==========================================================
<?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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<bean id="singer" class="com.spring.springidol.Singer">
<property name="song" value="Ringa Ringa Roses 1 2 3"/>
</bean>

<bean id="singer1" class="com.spring.springidol.Singer" abstract="true"/>

<bean id="singer2" class="com.spring.springidol.Singer" parent="singer1">
<property name="song" value="Singer 1 Ringa Ringa Roses 1 2 3"/>
</bean>

<bean id="audience" class="com.spring.springidol.Audience"/>

<aop:config>
<aop:aspect id="audienceAspect" ref="audience">
<aop:pointcut id="performance" expression="execution(* *(..))"/>
<aop:after method="applause" pointcut="execution(* *(..))"/>
<aop:before method="takeSeats" pointcut-ref="performance"/>
<aop:before method="switchOffPhone" pointcut-ref="performance"/>
<aop:after-throwing method="askRefund" pointcut-ref="performance"/>
</aop:aspect>
</aop:config>

</beans>


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 121

Problem is understood and fixed. I was using BeanFactory to initialize target Bean (for AOP) so AOP features were not getting initialized and invoked on Target bean Singer. Instead of that ApplicationContext need to be used. Used below code in main method and now aspects are getting invoked. Yeppy.

ApplicationContext appContext = new FileSystemXmlApplicationContext("src/java/com/spring/springidol/aop.xml");
 
wood burning stoves
 
subject: AOP not getting invoked on my Target Bean Method --> Singer.perform() method
 
Similar Threads
AOP in Apring 2.5 - Declare Aspects with AspectJ annotations
Spring 2.5 and AOP - BeanPostProcessor before instantiation of bean failed
Autoproxying @AspectJ aspects
spring AOP doesn't work
unable to call AOP method