tags:

views:

986

answers:

3

Hi,

I'm using Spring AOP. I'm giving my pointcuts like:

@Pointcut("execution(* com.demo.Serviceable+.*(..))")
public void serviceMethodCalls() {
}

Is it possible to avoid the in-place pointcut expression in Spring AOP?

Thanks, Veera.

A: 

is this what you want? proxy based aop: http://static.springframework.org/spring/docs/2.5.x/reference/aop-api.html sample

<bean id="settersAndAbsquatulateAdvisor" 
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
    <ref local="beanNameOfAopAllianceInterceptor"/>
</property>
<property name="patterns">
    <list>
        <value>.*set.*</value>
        <value>.*absquatulate</value>
    </list>
</property>

silmx
sample:<bean id="settersAndAbsquatulateAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="beanNameOfAopAllianceInterceptor"/> </property> <property name="patterns"> <list> <value>.*set.*</value> <value>.*absquatulate</value> </list> </property></bean>
silmx
+2  A: 

It depends on what style of AOP you choose with Spring. As you stick with the annotation based approach, there is not much you can get except having constants for the expressions located in an extenal class.

This is due to the @Aspect annotation based AOP style dedicated to colocate where and what. You can somehow get some configurability by using abstract method and binding the pointcut to it.

@Aspect
public abstract class MyAspect {

  protected abstract pointcut();


  @Before("pointcut()"
  public void myAdviceMethod() {
    // Advice code goes here
  }
}


public class ConcreteAspect extends MyAspect {

  @Pointcut("execution(* com.acme.*.*(..))")
  protected pointcut() {
  )
}

This is possible but seems rather awkward to me as the implementor has to know that @Pointcut is required on the method. If she forgets to place it, it will crash entirely.

If you need the flexibility to have advice and pointcut separated you better stick to the XML based configuration (see Spring documentation for example).

A kind of man in the middle is the possibility to tie your pointcut to a custom annotation and let the users decide where to place it and thus, when to get your advice applied. Spring documentation (chapter 6.2.3.4) gives more info on that.

Regards, Ollie

Oliver Gierke
+1  A: 

Pointcut expression is introduced in Spring2.X, which effectively allows you to match to place(s) where you want an advice be weaved into.

You can also use Spring1.X old fashion way, which is to create a ProxyFactoryBean(composing of 1 target object and potentionally multiple advices) through spring configuration xml as below. In this way, you won't need to mess with pointcut expression. The disadvantage of this though is you can only weave advice(s) into just one target object instead of multiple ones.

Here is one typical example below:

<bean id="personTarget" class="com.mycompany.PersonImpl">
    <property name="name"><value>Tony</value></property>
    <property name="age"><value>51</value></property>
</bean>

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
    <property name="someProperty"><value>Custom string property value</value></property>
</bean>

<bean id="person" 
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces"><value>com.mycompany.Person</value></property>

    <property name="target"><ref local="personTarget"/></property>
    <property name="interceptorNames">
        <list>
            <value>myAdvisor</value>
        </list>
    </property>
</bean>
Daniel