Skip to content
tylertreat edited this page Jan 21, 2013 · 4 revisions

The Before annotation indicates that the annotated advice method in an Aspect class is to be executed before a join point is invoked. The join point itself will be passed as an argument to methods annotated with this.

This advice does not have the ability to halt execution flow proceeding to the join point unless it throws an exception.

Annotation Attributes

Before has three attributes that can be used to specify advice metadata.

beans: declares the beans and, optionally, specific methods which make up a pointcut (i.e. where the annotated advice should be invoked with regard to specific beans) as an array.

A simple example that would apply the advice to a single bean named myBean would be @Before(beans = "myBean").

To apply the advice to all methods named foo in myBean, the method name is specified with a wildcard (*) character, e.g. @Before(beans = "myBean.foo(*)").

To apply the advice to a specific method foo in myBean which takes an integer and float as arguments, it would look like @Before(beans = "myBean.foo(java.lang.Integer, java.lang.Float)").

within: declares the packages such that any contained type's methods make up a pointcut (i.e. where the annotated advice should be invoked with regard to specific packages) as an array.

A simple example that would apply the advice to every method of every bean in the package com.foo.bar would be @Before(within = "com.foo.bar").

order: Declares the advice precedence. A smaller number indicates a higher precedence, while a larger number indicates a lower precedence. The default value is Integer.MAX_VALUE. The precedence determines the order in which advice is executed.

Before Example

The example below illustrates how a before advice method can be specified using the Before annotation.

@Before(beans = { "fooBean", "barBean.foo(*)", "barBean.bar(java.util.Date)" }, within = "com.foo.bar", order = 1)
public void beforeAdvice(JoinPoint joinPoint) {
    // ...
}
Clone this wiki locally