Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOP for all methods in a package

Tags:

java

spring

aop

I am using AOP for the first time. I have written the below AOP code which works fine when i use it to intercept a particular method.

Can somebody guide me - how I can set it up to intercept all methods in a certain package (com.test.model)?

Basically how to setup appcontext.xml.

Also, do i need to do something like below to call before calling each method?

AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass");
aoptest.addCustomerAround("dummy");

Can somebody help?

Please let me if some more explanation is needed.

Below is my code:

Interface:

package com.test.model;

import org.springframework.beans.factory.annotation.Autowired;

public interface AopInterface {


    @Autowired
    void addCustomerAround(String name);
}

Class:

package com.test.model;

import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class AopClass implements AopInterface {

    public void addCustomerAround(String name){
        System.out.println("addCustomerAround() is running, args : " + name);
    }
}

AOP:

package com.test.model;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;



@Aspect
public class TestAdvice{


     @Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
       public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("testAdvice() is running!");


       }
}

appcontext:

<!-- Aspect -->
    <aop:aspectj-autoproxy />
    <bean id="AopClass" class="com.test.model.AopClass" />
    <bean id="TestAdvice" class="com.test.model.TestAdvice" />
like image 391
fiddle Avatar asked Sep 28 '15 06:09

fiddle


People also ask

What is the difference between JoinPoint and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What is JoinPoint in AOP?

Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. Join point information is available in advice bodies by declaring a parameter of type org. aspectj.

Which method is executed first in AOP?

Executing method on the target class Thus, Spring AOP injects a proxy instead of an actual instance of the target class. When we start the Spring or Spring Boot application, we see Spring executes the advice before the actual method.


1 Answers

Just put:

@Around("execution(* com.test.model..*.*(..))")

The format of an execution expression is:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

where only ret-type-pattern, name-pattern and param-pattern are required, so at least we need an expression like:

execution(ret-type-pattern name-pattern(param-pattern))
  • ret-type-pattern matches the return type, * for any
  • name-pattern matches the method name, you can use * as wildcard and .. to indicate sub-package
  • param-pattern matches the method parameters, (..) for any number of parameters

You can find more information here: 10. Aspect Oriented Programming with Spring, there are some useful examples.

like image 101
Tobías Avatar answered Nov 02 '22 09:11

Tobías