<aop:aspectj-autoproxy/>You also need to add following libraries on class path
package com.dineshonjava.sdnext.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class LoggingAspect
{
//....
}
They will be configured in XML like any other bean as follows:<bean class="com.dineshonjava.sdnext.aop.aspect.LoggingAspect" id="logAspect"> <!-- configure properties of aspect here as normal --> </bean>
package com.dineshonjava.sdnext.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggingAspect
{
@Pointcut("execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployee(..))")
public void addEmployee(){}
}
The following example defines a pointcut named 'businessService' that will match the execution of every method available in the classes under the package com.dineshonjava.sdnext.aop.aspect.service:import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))") // expression
private void businessService() {} // signature
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
@Pointcut("within(com.dineshonjava.sdnext.aop.aspect.trading..*)")
private void inTrading() {}
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}
package com.dineshonjava.sdnext.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SystemArchitecture {
/**
* A join point is in the web layer if the method is defined
* in a type in the com.dineshonjava.sdnext.aop.aspect.web package or any sub-package
* under that.
*/
@Pointcut("within(com.dineshonjava.sdnext.aop.aspect.web..*)")
public void inWebLayer() {}
/**
* A join point is in the service layer if the method is defined
* in a type in the com.dineshonjava.sdnext.aop.aspect.service package or any sub-package
* under that.
*/
@Pointcut("within(com.dineshonjava.sdnext.aop.aspect.service..*)")
public void inServiceLayer() {}
/**
* A join point is in the data access layer if the method is defined
* in a type in the com.dineshonjava.sdnext.aop.aspect.dao package or any sub-package
* under that.
*/
@Pointcut("within(com.dineshonjava.sdnext.aop.aspect.dao..*)")
public void inDataAccessLayer() {}
/**
* A business service is the execution of any method defined on a service
* interface. This definition assumes that interfaces are placed in the
* "service" package, and that implementation types are in sub-packages.
*
* If you group service interfaces by functional area (for example,
* in packages com.dineshonjava.sdnext.aop.aspect.abc.service and com.dineshonjava.sdnext.aop.aspect.def.service) then
* the pointcut expression "execution(* com.dineshonjava.sdnext.aop.aspect..service.*.*(..))"
* could be used instead.
*
* Alternatively, you can write the expression using the 'bean'
* PCD, like so "bean(*Service)". (This assumes that you have
* named your Spring service beans in a consistent fashion.)
*/
@Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))")
public void businessService() {}
/**
* A data access operation is the execution of any method defined on a
* dao interface. This definition assumes that interfaces are placed in the
* "dao" package, and that implementation types are in sub-packages.
*/
@Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.dao.*.*(..))")
public void dataAccessOperation() {}
}
The pointcuts defined in such an aspect can be referred to anywhere that you need a pointcut expression. For example, to make the service layer transactional, you could write:<aop:config>
<aop:advisor advice-ref="tx-advice" pointcut="com.dineshonjava.sdnext.aop.aspect.SystemArchitecture.businessService()">
</aop:advisor></aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED">
</tx:method></tx:attributes>
</tx:advice>
@Before("businessService()")
public void doBeforeTask(){
...
}
@After("businessService()")
public void doAfterTask(){
...
}
@AfterReturning(pointcut = "businessService()", returning="retVal")
public void doAfterReturnningTask(Object retVal){
// you can intercept retVal here.
...
}
@AfterThrowing(pointcut = "businessService()", throwing="ex")
public void doAfterThrowingTask(Exception ex){
// you can intercept thrown exception here.
...
}
@Around("businessService()")
public void doAroundTask(){
...
}
@Before("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))")
public doBeforeTask(){
...
}
Labels: Spring AOP, Spring3.0