一:AOP
1:在程序原有的纵向执行流程中,针对某一个或者一些方法添加通知,形成横切面的过程就是切面编程。
2:常用概念
(1):原有功能:切点,pointcut
(2):前置通知:在切点之前执行的功能before advice
(3):后置通知:在切点后执行的功能after advice
(4):如果切点执行过程中出现异常,会触发异常通知throws advice
(5):所有功能总称叫做切面
(6):织入:把切面嵌入到原有功能中叫做织入
3:两种AOP方式
(1)Schema-based
(a):每个通知都需要实现接口和类
(b):配置Spring配置文件时在<aop:config>配置
(2):AspectJ
(a):每个通知不需要实现接口和类
(b):配置Spring配置文件是在<aop:config>的子标签<aop:aspect>配置
二:Schema-based的实现
1:前置通知和后置通知
(1):新建包advice,在advice包下新建类MyAfterAdvice
package advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvice implements AfterReturningAdvice{
/**
* arg0:切点方法返回值
* arg1:切点方法对象
* arg2:切点方法参数
* arg3:切点方法所在类的对象
*/
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行后置通知");
}
}
(2):在advice包下新建类MyBeforeAdvice类
package advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice{
/**
* arg0:切点方法对象Method对象
* arg1:切点方法参数
* arg2:切点在哪个对象中
*/
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
// TODO Auto-generated method stub
System.out.println("执行前置通知");
}
}
(3):src下新建test包,在test包下新建类Demo
package test;
public class Demo {
public void demo1() {
System.out.println("我是Demo类的方法demo1()");
}
public void demo2() {
System.out.println("我是Demo类的方法demo2()");
}
public void demo3() {
System.out.println("我是Demo类的方法demo3()");
}
}
(4)在src目录下新建applicationContext.xml,并配置
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置demo类,测试使用 -->
<bean id="demo" class="test.Demo"></bean>
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo2())" id="mypoint1"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint1"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint1"/>
</aop:config>
</beans>
(5):src下新建mainTest包,mainTest包下新建MainTest类并运行
package mainTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.Demo;
public class MainTest {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo=ac.getBean("demo",Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
2:异常通知
(1)在advice包下新建MyThrow类
package advice;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class MyThrow implements ThrowsAdvice{
/**
* 两个方法只能出现一个
* @param ex
* @throws Throwable
*/
// public void afterThrowing(Method m,Object[] args,Object targer,Exception ex) {
// System.out.println("执行异常通知schema-base方式");
// }
public void afterThrowing(Exception ex) throws Throwable{
System.out.println("执行异常通知schema-base方式");
}
}
(2):在applicationContext.xml更新配置如下
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置demo类,测试使用 -->
<bean id="demo" class="test.Demo"></bean>
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo2())" id="mypoint1"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint1"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint1"/>
</aop:config>
<!-- 配置异常通知类对象,在切面中引入 -->
<bean id="mythrow" class="advice.MyThrow"></bean>
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo1())" id="mypoint2"/>
<!-- 通知 -->
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint2"/>
</aop:config>
</beans>
(3):当Demo类的demo1()方法出现异常时,将执行MyThrow异常通知
3:环绕通知
(1):在advice包下新建MyAround类
package advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAround implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
System.out.println("环绕-前置");
Object result=arg0.proceed();
System.out.println("环绕-后置");
return result;
}
}
(2):在applicationContext.xml中更新配置如下
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置demo类,测试使用 -->
<bean id="demo" class="test.Demo"></bean>
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo2())" id="mypoint1"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint1"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint1"/>
</aop:config>
<!-- 配置异常通知类对象,在切面中引入 -->
<bean id="mythrow" class="advice.MyThrow"></bean>
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo1())" id="mypoint2"/>
<!-- 通知 -->
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint2"/>
</aop:config>
<!-- 配置环绕通知类对象,在切面中引入 -->
<bean id="myaround" class="advice.MyAround"></bean>
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo3())" id="mypoint3"/>
<!-- 通知 -->
<aop:advisor advice-ref="myaround" pointcut-ref="mypoint3"/>
</aop:config>
</beans>
(3):运行MainTest类,结果如下
三:AspectJ方式(新建一个项目)
1:异常通知
(1):新建包advice,在advice包下新建类MyThrowAdvice
package advice;
public class MyThrowAdvice {
public void myException(Exception e1) {
System.out.println("执行异常通知"+e1.getMessage());
}
}
(2):新建包test,在test包下新建类Demo类
package test;
public class Demo {
public void demo1() {
System.out.println("我是Demo类的方法demo1()");
}
public void demo2() {
System.out.println("我是Demo类的方法demo2()");
}
public void demo3() {
System.out.println("我是Demo类的方法demo3()");
}
}
(3):新建包mainTest,在mainTest包下新建MainTest类
package mainTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.Demo;
public class MainTest {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo=ac.getBean("demo",Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
(4):在src目录下新建配置文件applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置demo类,测试使用 -->
<bean id="demo" class="test.Demo"></bean>
<!-- 配置异常通知类 -->
<bean id="mythrow" class="advice.MyThrowAdvice"></bean>
<aop:config>
<aop:aspect ref="mythrow">
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo1())" id="mypoint1"/>
<aop:after-throwing method="myException" pointcut-ref="mypoint1" throwing="e1"/>
</aop:aspect>
</aop:config>
</beans>
(5):执行MainTest当Demo类的demo1()方法异常时,会触发异常通知
2:环绕通知
(1):advice包下新建MyAroundAdvice类
package advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAroundAdvice {
public void myBefore1(){
System.out.println("前置1");
}
public void myBefore2(){
System.out.println("前置2");
}
public void myAfter1(){
System.out.println("后置1");
}
public void myAfter2(){
System.out.println("后置2");
}
public void myThrow(){
System.out.println("异常");
}
public Object myAround(ProceedingJoinPoint p) throws Throwable {
System.out.println("执行环绕");
System.out.println("环绕前置");
Object result=p.proceed();
System.out.println("环绕后置");
return result;
}
}
(2):更新applicationContext.xml的配置信息
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置demo类,测试使用 -->
<bean id="demo" class="test.Demo"></bean>
<!-- 配置异常通知类 -->
<bean id="mythrow" class="advice.MyThrowAdvice"></bean>
<aop:config>
<aop:aspect ref="mythrow">
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo1())" id="mypoint1"/>
<aop:after-throwing method="myException" pointcut-ref="mypoint1" throwing="e1"/>
</aop:aspect>
</aop:config>
<!-- 配置通知类 -->
<bean id="myaround" class="advice.MyAroundAdvice"></bean>
<aop:config>
<aop:aspect ref="myaround">
<!-- 配置切点 -->
<aop:pointcut expression="execution(* test.Demo.demo1())" id="mypoint1"/>
<aop:pointcut expression="execution(* test.Demo.demo2())" id="mypoint2"/>
<aop:pointcut expression="execution(* test.Demo.demo3())" id="mypoint3"/>
<!-- 配置前置通知 -->
<aop:before method="myBefore1" pointcut-ref="mypoint1"/>
<aop:before method="myBefore2" pointcut-ref="mypoint2"/>
<!-- 配置后置通知 -->
<aop:after method="myAfter1" pointcut-ref="mypoint1"/>
<!-- <aop:after-throwing/> 异常时执行-->
<!-- 后置通知,切点正确执行时执行 -->
<aop:after-returning method="myAfter2" pointcut-ref="mypoint2"/>
<!-- 配置环绕通知 -->
<aop:around method="myAround" pointcut-ref="mypoint3"/>
</aop:aspect>
</aop:config>
</beans>