Pankaj
Pankaj

Reputation: 3664

Intercept only public methods while using around advce

I have a service which has few public and few private method. Note that i don't have interface for this service.

package com.myservice.rest;

public class CustomerService {

 public Customer getCustomerbyId(String id){
 ...................
  .............
 }
 public Customer getCustomerbySSN(String SSN){

 }

 private boolean verfiyCustomer(){
 }
}

I have aspect which has Around advice. I want to intercept all public method.

@Aspect
@Component
public class ApplicationMonitoring {

@Around("execution(public com.myservice.rest.CustomerService.*(..))")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {

 }

I'm getting error while building through maven name pattern expected. However if i don't use return type as public and if i use wildcard (*), it intercepts all private method as well which i don't want.

Upvotes: 0

Views: 1038

Answers (2)

Pankaj
Pankaj

Reputation: 3664

I could achieve this by adding return type as (*)

@Around("execution(public * com.myservice.rest.CustomerService.*(..))")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {

 }

Pattern:

<AccessModifier> <ReturnType> <Package/Class> <Method>(<Parameters>)

Upvotes: 1

mvb13
mvb13

Reputation: 1584

Spring documentation says you should:
1 Create pointcut:

    @Pointcut("execution(public * *(..))")
    public void anyPublicOperation() {}

2 Use this pointcut in your advice:

    @Aspect
    @Component
    public class ApplicationMonitoring {

    @Around("execution(com.myservice.rest.CustomerService.*(..)) && path_to_class_with_pointcut.anyPublicOperation()")
    public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {

    }

Upvotes: 0

Related Questions