Spring AOP + AspectJ in XML configuration example
Example
Here is the content for pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.spring</groupId>
<artifactId>aop-aspect-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>aop-aspect-example</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring AOP + AspectJ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<!-- JavaConfig need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.spring</groupId>
<artifactId>aop-aspect-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>aop-aspect-example</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring AOP + AspectJ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<!-- JavaConfig need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
Here is the content for Instrumentlist.java
package com.mahesh.training.aop;
public class Instrumentlist {
public void play() throws Exception
{
System.out.println("Playing the instrument");
//throw new Exception("problem with instrument");
}
public void secondPlay() {
System.out.println("second play");
}
}
Here is the content for Audience.java
package com.mahesh.training.aop;
public class Audience {
public void takeSeats()
{
System.out.println("taking seats");
}
public void swithOffMobilePhones()
{
System.out.println("switch off mobile phones");
}
public void applaud()
{
System.out.println("clapping");
}
public void demandRefund()
{
System.out.println("not satified -- demand refund");
}
public void returningHome(){
System.out.println("returning Home");
}
}
Here is the Spring Configuration file applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="instrumentlist" class="com.mahesh.training.aop.Instrumentlist">
</bean>
<bean id="audience" class="com.mahesh.training.aop.Audience"></bean>
<aop:config>
<aop:pointcut expression="target(com.mahesh.training.aop.Instrumentlist)" id="instPointCut"/>
<aop:aspect ref="audience">
<aop:before method="takeSeats" pointcut-ref="instPointCut"></aop:before>
<aop:before method="swithOffMobilePhones" pointcut-ref="instPointCut"></aop:before>
<aop:after-returning method="applaud" pointcut-ref="instPointCut"></aop:after-returning>
<aop:after-throwing method="demandRefund" pointcut-ref="instPointCut"></aop:after-throwing>
<aop:after method="returningHome" pointcut-ref="instPointCut"></aop:after>
</aop:aspect>
</aop:config>
</beans>
Here is the Test class App.java
package clientpack;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.training.aop.Instrumentlist;
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
Instrumentlist inst = (Instrumentlist)ctx.getBean("instrumentlist");
inst.play();
}
}
Output
taking seats
switch off mobile phones
Playing the instrument
clapping
returning Home
switch off mobile phones
Playing the instrument
clapping
returning Home
No comments:
Post a Comment