2

I am trying to @Before in AOP. I have a dto where I implemented an interface and in the controller method the dto is one of the parameter and since it implemented the interface I want to identify the parameter with the implemented interface in the point cut.I have tried the following

Employee .java

package com.example.demo;

public class Employee implements UserData {

private String name;
private String id;
private String clazz;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getClazz() {
    return clazz;
}

public void setClazz(String clazz) {
    this.clazz = clazz;
}

@Override
public String getData() {
    String data=name+"_"+id;
    return data;
}

}

Controller

@RestController
public class HelloController {

@RequestMapping("/")
public String getEmployees(Employee emp) {
    return "emp";
}

interface

public interface Tenant {

String getData();
}

Aspect

@Before("pointcut(tenant)")
public void before(Tenant tenant){


}   
@Pointcut("execution(* com.example.demo.*.*(UserData))"+" && 
 args(UserData))")
public void pointcut(UserData UserData) {
    System.out.println("In aspect********************"+UserData.getData());
}
}

here in the above aspect UserData is nt identified as instance of Employee..How to achieve that? I can put (..) in the point cut but I want it tobe specific to UserData.

yacky
  • 313
  • 1
  • 6
  • 19

2 Answers2

0

You have provided wrong argument with @Before annotation. We need to provide JoinPoint as argument.

  @Before("execution(* com.example.demo.HelloController.getEmployees*(UserData))")
    public void before(JoinPoint jp){
    System.out.println("In aspect********************");

}

JointPoint has method getArgs(), you can use it to fetch the parameters.

Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
0

User Gaurav Srivastav was right about the JoinPoint parameter for your advice, but you neither need the * in getEmployees* nor getArgs() if you just want to limit pointcut matching.

You either need to use a fully qualified class name (incl. package) in order to specify the argument type ...

@Before("execution(* com.example.demo.HelloController.getEmployees(com.example.demo.UserData))")
public void before(JoinPoint thisJoinPoint) {
  System.out.println(thisJoinPoint);
}

... or, if you need access to the method parameter you can bind it via args() pointcut designator:

@Before(
  "execution(* com.example.demo.HelloController.getEmployees(*)) && " + 
  "args(userData)"
)
public void before(JoinPoint thisJoinPoint, UserData userData) {
  System.out.println(thisJoinPoint);
  System.out.println("  " + userData);
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I have updated my point cut.I want to identify specific to UserData as it will be implemented by Employee.There will be many other classes which implement USerData. So I want this point to work for all those cases @kriegaex – yacky Jun 11 '18 at 05:45
  • Sorry, I do not understand your question, I can only speculate what you might want to ask. Please elaborate and explain more clearly. You could also evolve the code into an [MCVE](http://stackoverflow.com/help/mcve) (incl. main class and configuration) that I can run and which shows me what you want to do. – kriegaex Jun 11 '18 at 15:49