Agenda
Event Handling in Spring
Introduction to AOP
Writing Aspects
Pointcut and WildCard Expressions
Reusing pointcut definition
After advice
Around advice
Custom advice annotation
Spring Events
Built In Spring events
Publishing Spring Events
public class Dummy {
public void display() {
System.out.println("Display");
}
}
<bean id="dummy" class="com.spring.demo.Dummy"/>
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
ctx.start();
Dummy dummy = ctx.getBean("dummy", Dummy.class);
dummy.display();
ctx.stop();
Creating an Event Listener for Spring events
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MyEventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println(event);
}
}
<bean class="com.spring.event.MyEventListener"/>
Capture a specific spring event
package com.spring.event;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class MyEventListener implements ApplicationListener<ContextStartedEvent> {
@Override
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println(event);
}
}
Creating a custom Spring Event
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
CustomEvent(Object object) {
super(object);
}
}
import com.spring.CustomEvent;
import org.springframework.context.ApplicationListener;
public class CustomEventListener implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent event) {
System.out.println(event.getSource());
}
}
Create custom Event Listener
<bean class="com.spring.listener.CustomEventListener"/>
Introduce ApplicationEventPublisher
import com.spring.CustomEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class Dummy implements ApplicationEventPublisherAware {
ApplicationEventPublisher applicationEventPublisher;
void display() {
CustomEvent customEvent = new CustomEvent(this);
applicationEventPublisher.publishEvent(customEvent);
System.out.println("Display");
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
} }
Using event Listener annotation
import org.springframework.context.event.*;
public class MultipleEventListener {
@EventListener(ContextStartedEvent.class)
void start() {
System.out.println("----------------start");
}
@EventListener(ContextStoppedEvent.class)
void stopped() {
System.out.println("----------------stopped");
}
}
<context:annotation-config/>
Running spring event in separate Thread
<bean id="simpleAsyncTaskExecutor"
class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
<bean id="applicationEventMulticaster"
class="org.springframework.context.event.SimpleApplicationEventMulticaster">
<property name="taskExecutor" ref="simpleAsyncTaskExecutor"/>
</bean>
Advantages of Spring Events
AOP (Aspect Oriented Programming)
AOP terminologies
AOP in action
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy/>
</bean>
AOP in action (Cont.)
class Dummy{
public void display(){
System.out.println(“Dummy class display method“);
}}
<bean id="dummy" class="com.spring.demo.Dummy">
AOP in action (Cont.)
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("execution(public void display())")
void beforeAdvice() {
System.out.println("Before advice is running");
}
}
Wild Expressions for AOP
@Before("execution( void display())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("execution( * get*())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("execution( * com.spring.demo.Dummy.*())")
void beforeAdvice() {
System.out.println("Running before advice");
}
Wild Expressions for AOP (Cont.)
@Before("execution( * com.spring.demo.Dummy.*())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("execution(void *())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("execution(* *())")
void beforeAdvice() {
System.out.println("Running before advice");
}
Wild Expressions for AOP (Cont.)
@Before("execution(* *(..))")
void beforeAdvice() {
System.out.println("Running before advice");
}
Types of Advice
@Before("execution(void display())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@AfterReturning(pointcut = "execution(Integer getInteger())", returning = "returnValue")
void afterReturningAdvice(Integer returnValue) {
System.out.println("Running AfterReturning " + returnValue);
}
@AfterThrowing(pointcut = "execution(void throwException())", throwing = "ex")
void afterReturningAdvice(Exception ex) {
System.out.println("Running AfterThrowing " + ex);
}
Types of Advice (Cont.)
@After("execution(void display())")
void afterAdvice(){
System.out.println("Running after advice");
}
@Around("execution(void display())")
void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around before");
proceedingJoinPoint.proceed();
System.out.println("Around after");
}
Pointcut Expressions
@Before("execution(void display())")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("within(com.spring.demo.*)")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("bean(dummy))")
void beforeAdvice() {
System.out.println("Running before advice");
}
Pointcut Expressions (Cont.)
@Before("args(Integer)")
void beforeAdvice() {
System.out.println("Running before advice");
}
@Before("this(com.spring.demo.Dummy)")
void beforeAdvice() {
System.out.println("Running before advice");
}
Reusing pointcut
@Before("dummyClassPointcut() || getStringPointcut()")
void beforeAdvice() {
System.out.println("Running before advice");
}
@After("displayPointcut()")
void afterAdvice(){
System.out.println("Running after advice");
}
@Pointcut("execution(void display())")
void displayPointcut(){}
@Pointcut("execution(String getString())")
void getStringPointcut(){}
Accessing JoinPoint in advice
@Before("execution(Integer getInteger(Integer))")
void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Running before advice");
System.out.println(joinPoint);
System.out.println(joinPoint.getThis());
System.out.println(joinPoint.getSignature());
Object [] objects=joinPoint.getArgs();
for (Object object:objects){
System.out.println(object);
}
}
Use cases of AOP