XML方式   

beans.xml配置如下:

<context:component-scan base-package="com.bjsxt"/>
<!-- <aop:aspectj-autoproxy/> -->
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>//注意需要把切面类初始化
<aop:config>

//指定pointcut
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" id="servicePointcut"/>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut-ref="servicePointcut"/>
</aop:aspect>

</aop:config>

 

或者   不指定pointcut

<aop:config>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))"/>
</aop:aspect>

</aop:config>

 

注解方式:

@Aspect
@Component
public class LogInterceptor {


/***********************************************************
* @author zhaokuan
* 测试@Pointcut 测试切面集合
*
***********************************************************/
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};

@Before("myMethod()")
public void before(){
System.out.println("method start1");
}
@AfterReturning("myMethod()")
public void afterReturning(){
System.out.println("method afterReturning1");
}

@Around("myMethod()")
public void s(){
System.out.println("Around myMethod start");
}

 }

配置文件只需要配置一句

<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy/>//自动