1
votes

I am studying for the Spring Core certification and I have the following doubts try to understand how AOP Context Selecting Pointcuts work and for what pourposes it is used for.

So looking on the documentation I read that pointcuts can also select useful join point context instead the simple case represented by the call of a method execution associated to a specific advice.

Such as something like:

  • The currently executing object (proxy)
  • The target object
  • Method arguments
  • Annotations associated with the method, target, or arguments

So it seems to me that it is used in this way to do something more than something like automatically perform this operation when this method is invoked, is it true?

Now on the documentation there is the followings 2 examples (and I have some difficulties to understand) related to the CONTEXT SELECTING.

Consider the following requirements: Log a message every time Server is about to start

public interface Server {
    public void start(Map input);
    public void stop();
}

In the advice, how do we access to Server? and to map?

1) FIRST EXAMPLE: WITHOUT CONTEXT SELECTION:

On the documentation it say that All needed info must be obtained from JoinPoint object, in this way:

@Before(“execution(void example.Server.start(java.util.Map))”)
public void logServerStartup(JoinPoint jp) {
    // A 'safe' implementation would check target type
    Server server = (Server) jp.getTarget();
   // Don't assume args[0] exists
   Object[] args= jp.getArgs();
   Map map = args.length > 0 ? (Map) args[0] : new HashMap();
   logger.info( server + “ starting – params: “ + map);
}

So I think that this means something like: every time that it is executed the start() method on my Server perform the below logServerStartup() advice method, it is correct?

Now it seems to me that it extract the Server information and the Map input parameter content by the JointPoint object.

What exatly represent this object? Is it an AspectJ object or is it related to a Spring AOP object? What contains and how it work?

2) SECOND EXAMPLE: WITHOUT SELECTION:

@Before(“execution(void example.Server.start(java.util.Map)) && target(server) && args(input)”)
public void logServerStartup(Server server, Map input) {
    …
}

So it seems to me that in this pointcut I am also including some information contained into my application context as the server and the input parameter (the Map object) related to the execution of the jointpoint (when the start() method is called on the Server object).

Is it correct? What exactly do the target(server) method?

Can you explain me how exactly works?

Tnx

1
Andrea, can you please use one account on StackOverflow? You seem to use several. Same name, but different accounts.kriegaex
opsss...for an error I think that I am logged using my other email address !!! Now I will come back to use only the main one !!!user4353372

1 Answers

1
votes

Are you just studying documentation or maybe also writing and testing some code for yourself? That would help you a lot. Actually the second example is preferable to the first one because it binds the desired parameters in the pointcut and they are easily accessible to you via advice parameters. No more boilerplate code to determine them by yourself via joinpoint properties. Otherwise the examples are equivalent.

Even if you just use Spring AOP, its syntax is a subset of AspectJ syntax and so it uses a few AspectJ classes. Just check your imports' package names and you will easily see which classes come from Spring and which ones come from AspectJ.

What a joinpoint represents is a place in your code (e.g. before or after method execution) which can be selected by a pointcut and then utilised to inject other code via an advice.