package com.javavillage.annotate; import org.springframework.stereotype.Component; @Component("inst") public class Instrumentalist { public void play() { System.out.println("Playing instrument"); //throw new RuntimeException("problem with instrument"); } }
@Aspect: indicates class is aspect. @Before: Executes before play method @After: Executes after the method returned a result @AfterReturning: Executes after play method @Around: Executes around the method execution, combine all three advices above. @AfterThrowing:Executes after play method if any exception comes
package com.javavillage.annotate; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class Audience { @Before("execution (* com.javavillage.annotate.*.*(..))") public void takeSeats() { System.out.println("taking seats"); } @Before("execution (* com.javavillage.annotate.*.*(..))") public void switchOffMobilePhones() { System.out.println("switching off mobile phones"); } @AfterReturning("execution (* com.javavillage.annotate.*.*(..))") public void applaud() { System.out.println("clapping"); } @AfterThrowing("execution (* com.javavillage.annotate.*.*(..))") public void demandRefund() { System.out.println("demanding refund"); } }
<?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:context="http://www.springframework.org/schema/context" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.javavillage.annotate"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency>
package com.javavillage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javavillage.annotate.Instrumentalist; /** * Hello world! * */ public class App { public static void main( String[] args ) { ApplicationContext ctx=new ClassPathXmlApplicationContext("aopContext.xml"); Instrumentalist instrumentalist=(Instrumentalist)ctx.getBean("inst"); instrumentalist.play(); } }
Execute Spring Aspect Application:
throw new RuntimeException("problem with instrument"); from Instrumentalist and see how @AfterThrowing annotated method will get executed from Aspect class(Audience.java).