AOP中的通知可分为:前置,后置,异常,环绕,最终通知,在spring中,可以使用两种方式来实现这个技术:
1.使用xml的配置实现
①引入AOP命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>
②引入AOP(@Aspect)支持
<aop:aspectj-autoproxy/>
③定义一个类,在xml中指明切面
package com.heying.service;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyIntercepter{
public void doAccessCheck(/*String userName*/){
System.out.println("+++++++前置通知+++++++"); // 前置通知在执行业务方法前通知
}
public void doAfterReturning(/*String userName*/){
System.out.println("+++++++后置通知+++++++"); // 前置通知在执行业务方法前通知
}
public void doAfter(/*String userName*/){
System.out.println("+++++++最终通知+++++++"); // 前置通知在执行业务方法前通知
}
public void doAfterThrowing(/*String userName*/){
System.out.println("+++++++例外通知+++++++"); // 前置通知在执行业务方法前通知
}
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("====>>>>into function");
Object object = proceedingJoinPoint.proceed();
System.out.println("====>>>>out function");
return object;
}
}
④配置xml文件:
<bean id="personService" class="com.heying.service.impl.PersonServiceBean"/>
<bean id="aspetBean" class="com.heying.service.MyIntercepter"/>
<aop:config>
<aop:aspect id="aspect" ref="aspetBean">
<aop:pointcut id="mycut" expression="execution(* com.heying.service.impl.PersonServiceBean.*(..))"/>
<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<aop:around pointcut-ref="mycut" method="doAround"/>
</aop:aspect>
</aop:config>
⑤测试:
@Test
public void aopTest(){
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
PersonService personService = (PersonService) applicationContext.getBean("personService");
personService.save("heying1234");
} catch (Exception e) {
e.printStackTrace();
}
}
2.使用注解方式实现
①引入AOP命名空间:如上
②引入AOP(@Aspect)支持: 如上
③定义一个类,指明切面
package com.heying.service;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect // 指明这个类是一个切面
public class MyIntercepter{
//execution(*返回值类型 com.拦截的包 ..子包 *.* 类.方法(..方法的参数))
@Pointcut("execution(* com.heying.service.impl.PersonServiceBean.*(..))")
private void anyMethod() {} // 1. 定义切入点
@Before("anyMethod()")
public void doAccessCheck(/*String userName*/){
System.out.println("+++++++前置通知+++++++"); // 前置通知在执行业务方法前通知
}
@AfterReturning("anyMethod()")
public void doAfterReturning(/*String userName*/){
System.out.println("+++++++后置通知+++++++"); // 前置通知在执行业务方法前通知
}
@After("anyMethod()")
public void doAfter(/*String userName*/){
System.out.println("+++++++最终通知+++++++"); // 前置通知在执行业务方法前通知
}
@AfterThrowing("anyMethod()")
public void doAfterThrowing(/*String userName*/){
System.out.println("+++++++例外通知+++++++"); // 前置通知在执行业务方法前通知
}
@Around("anyMethod()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("====>>>>into function");
Object object = proceedingJoinPoint.proceed();
System.out.println("====>>>>out function");
return object;
}
}
④把切面和serviceBean交给spring管理
<bean id="myIntercepter" class="com.heying.service.MyIntercepter"/>
<bean id="personService" class="com.heying.service.impl.PersonServiceBean"/>
⑤测试:
@Test
public void aopTest(){
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
PersonService personService = (PersonService) applicationContext.getBean("personService");
personService.save("test123");
} catch (Exception e) {
e.printStackTrace();
}
}