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>