danny.lesnik
danny.lesnik

Reputation: 18639

Spring AOP setAdvice only on one specific Method

I have the following class:

package x.y.z;

public class MyClass{
public void someMethod(SomeObject object){
//do somethinng
}

public void {
//do somethinng
}

}

Now I would like to set @PointCutonly on method someMethod(SomeObject object, int param1)

How can I do it?

Update I'm trying

@Pointcut("execution(x.y.z.MyClass.someMethod(x.y.z.SomeObject))") but I'm getting not well formed pointcut exception.

Upvotes: 6

Views: 10788

Answers (3)

Abhishek Sengupta
Abhishek Sengupta

Reputation: 3301

Suppose we have a @Controller class :

package co.gg.controller;

@Controller
public class LoginController {

    @GetMapping(value="/getTenantRoles")
    @ResponseBody
    public String getTenantRoles(@RequestParam("tenant_id") String tenant_id) throws SQLException {

        sqlConnection = con.makeconnection(dbName, dbPort, dbUserName, dbPass, dbHost);

        String sql = "select * from tenant_role where tenant_id = "+tenant_id+" and is_active = 1 and is_deleted = 0;";
        PreparedStatement preparedStatement = sqlConnection.prepareStatement(sql);
        ResultSet rs = preparedStatement.executeQuery();

        ArrayList<TenantRoleModel> tenantRoleModelList = new ArrayList<>();
        while(rs.next()) {
            TenantRoleModel currentModel = new TenantRoleModel(
                                                                    rs.getInt("tenant_role_id"),
                                                                    rs.getInt("is_deleted"),
                                                                    rs.getInt("is_active"),
                                                                    rs.getInt("tenant_id"),
                                                                    rs.getString("role_name"),
                                                                    rs.getString("create_date")
                                                                );

            tenantRoleModelList.add(currentModel);
        }

        return new Gson().toJson(tenantRoleModelList);
    }
}

Then We have to intercept that method like this:

package co.gg.AOP;


@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class Aspects {

    @AfterReturning(value = "execution(* co.fdbk.controller.LoginController.getTenantRoles(..))", returning = "result")
    public void afterAPICall(JoinPoint joinPoint,Object result){

        System.out.println("AOP Method Name :"+ joinPoint.getSignature().getName());
        System.out.println("AOP Return Value  :"+ result);


    }
}

This is how we can intercept specific method.

Upvotes: 1

Michael Wiles
Michael Wiles

Reputation: 21184

Point cut should be:

target(x.y.z.MyClass) && execution(<RETURN TYPE> someMethod(x.y.z.SomeObject))

Upvotes: 11

Stanislav Levental
Stanislav Levental

Reputation: 2235

Attach AspectJ to your classpath and use maven AOP plugin to compile this Aspect to bytecode, look at this example:

@Aspect
public class IOControlAspect {
    @Around("execution(com...SomeClass.someMethod(*))")
    public Object ioControlWrapper(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        {your code here}
        return thisJoinPoint.proceed();
    }
}

Upvotes: 4

Related Questions