Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:41:16+00:00 2026-05-14T15:41:16+00:00

I need to create an aspect with a pointcut matching a method if: Is

  • 0

I need to create an aspect with a pointcut matching a method if:

  • Is public
  • Its class is annotated with @Controller (Finally does not)
  • One of its parameters (can have many) is annotated with @MyParamAnnotation.

I think the first two conditions are easy, but I don’t know if its possible to accomplish the third with Spring. If it is not, maybe I can change it into:

  • One of its parameters is an instance of type com.me.MyType (or implements some interface)

Do you think it’s possible to achieve this? And will performance be good?

Thanks

EDIT: One example of a matching method. As you can see, MyMethod is not annotated (but it can be).

@Controller
public class MyClass {
    public void MyMethod (String arg0, @MyParamAnnotation Object arg1, Long arg3) {
        ...
    }
}

EDIT: The solution I finally used, based on @Espen answers. As you can see, I changed my conditions a little: class doesn’t actually need to be a @Controller.

@Around("execution(public * * (.., @SessionInject (*), ..))")
public void methodAround(JoinPoint joinPoint) throws Exception {
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:41:16+00:00Added an answer on May 14, 2026 at 3:41 pm

    It was an interesting problem, so I created a little sample application to solve the case! (And improved it with Sinuhe’s feedback afterwards.)

    I have created a DemoController class that should work as an example for the aspect:

    @Controller
    public class DemoController {
    
        public void soSomething(String s, @MyParamAnnotation Double d, Integer i) {
        }
    
        public void doSomething(String s, long l, @MyParamAnnotation int i) {
        }
    
        public void doSomething(@MyParamAnnotation String s) {
        }
    
        public void doSomething(long l) {
        }
    }
    

    The aspect that will add a join point on the first three methods, but not the last method where the parameter isn’t annotated with @MyParamAnnotation:

    @Aspect
    public class ParameterAspect {
    
        @Pointcut("within(@org.springframework.stereotype.Controller *)")
        public void beanAnnotatedWithAtController() {
        }
    
        @Pointcut("execution(public * *(.., @aspects.MyParamAnnotation (*), ..))")
        public void methodWithAnnotationOnAtLeastOneParameter() {
        }
    
        @Before("beanAnnotatedWithAtController() " 
                + "&& methodWithAnnotationOnAtLeastOneParameter()")
        public void beforeMethod() {    
            System.out.println("At least one of the parameters are " 
                      + "annotated with @MyParamAnnotation");
        }
    }
    

    The first pointcut will create a joinpoint on all methods inside classes marked with @Controller.

    The second pointcut will add a joinpoint when the following conditions are met:

    • public method
    • first * is a wildcard for every return type.
    • second * is a wildcard for all methods in all classes.
    • (.., matches zero to many parameters of any type before the annotated parameter.
    • @aspects.MyParamAnnotation (*), matches a parameter annotated with the given annotation.
    • ..) matches zero to many parameters of any type after the annotated parameter.

    Finally, the @Before advice advises all methods where all conditions in both pointcuts are satisfied.

    The pointcut works with both AspectJ and Spring AOP!

    When it comes to performance. The overhead is small, especially with AspectJ that does the weaving on compile-time or load-time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.