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