注解配置
/切面的配置
@Aspect
public class MyAspect {
//切点
@Pointcut("execution(* com.ujiuye.service.*ServiceImpl.*(..))")
public void pt(){}
@Before("pt()")
public void before(){
System.out.println("这是前置通知");
}
@Around("pt()")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("这是环绕通知之前");
Object obj = point.proceed();
System.out.println("这是环绕通知之后");
return obj;
}
@After("pt()")
public void after(){
System.out.println("这是后置通知(无论是否出现异常都会执行)");
}
@AfterReturning("pt()")
public void afterReturnning(){
System.out.println("这是后置通知,(如果有异常不走)");
}
@AfterThrowing("pt()")
public void afterThrowing(){
System.out.println("捕获到了异常");
}
}
xml配置
<!--让spring 扫描aop的注解,自动配置aop-->
<aop:aspectj-autoproxy/>
<bean class="com.ujiuye.aspect.MyAspect"/>
<bean id="personService" class="com.ujiuye.service.PersonServiceImpl">
<property name="dao" ref="personDao"/>
</bean>
<bean id="personDao" class="com.ujiuye.dao.PersonDaoImpl"/>