we are going to create one application with the help of previous AOP terminology if you have not looked it before you can check it out here.


so the application scenario is like there is one orchestra in which the instrument gets played like a guitar and people applaud for that.
The event incident is like:
1)Before starting the Orchstertra people will take their seats.
2)Before the Orchstertra start audience will turn off their phone.
3)The performer will play the guitar with a sound as STRUM.
4)People will applaud with CLAP for the performance.

we are going to use the AspectJ for this application you can find out the maven dependency for the same below.

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.0.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.8</version>
    </dependency>

</dependencies>
public interface Instrument {
public void play();
}

Interface to implement by the instrument with play method.

public class Guitar implements Instrument {
public void play() {
System.out.println("Strum strum strum");
}
}

Here we are going to play the Guitar Instrument with Strum sound.

public interface Performer {
    void perform() throws PerformanceException;
}

public class PerformanceException extends Exception {
    public PerformanceException() {
        super();
    }

    public PerformanceException(String message) {
        super(message);
    }

}

The method perform will be used for the performance and if anything goes wrong it would be handled by the performance exception.

public class Instrumentalist implements Performer {
    private Instrument instrument;

    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }

    public Instrument getInstrument() {
        return instrument;
    }

    public void perform() throws PerformanceException {
        instrument.play();
    }
}

Here we have created the instrumentalist to be performed by the performer by the given instrument.

@Aspect
public class Audience {
    @Pointcut("execution(* com.springaop.Performer.perform(..))")
    public void performance() {
    }

    @Before("performance()")
    public void takeSeats() {
        System.out.println("The audience is taking their seats.....");
    }

    @Before("performance()")
    public void turnOffCellPhones() {
        System.out.println("The audience is turning off their cellphones");
    }

    @AfterReturning("performance()")
    public void applaud() {
        System.out.println("CLAP CLAP CLAP CLAP CLAP");
    }

    @AfterThrowing("performance()")
    public void demandRefund() {
        System.out.println("Boo! We want our money back!");
    }

    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint joinpoint) {
        try {
            System.out.println("***The audience is taking their seats***");
            System.out
                    .println("***The audience is turning off their cellphones***");
            long start = System.currentTimeMillis();

            joinpoint.proceed();

            long end = System.currentTimeMillis();
            System.out.println("***CLAP CLAP CLAP CLAP CLAP***");
            System.out.println("***The performance took:" + (end - start)
                    + " milliseconds***");
        } catch (Throwable e) {
            System.out.println("***Boo! We want our money back***!");
        }
    }
}

we are going to implement the different aspects based on the different events.

Output:

The audience is taking their seats
The audience is turning off their cellphones
The audience is taking their seats…..
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took:0 milliseconds
CLAP CLAP CLAP CLAP CLAP

Leave a comment