A .AOP
1, Name: Oriented Programming (Aspect Oriented Programming)
2, the normal flow of program execution flow is executed longitudinal
Oriented Programming, added in the original cross-section of the longitudinal execution process
without modifying the existing program code
scalability
original function quite to release some logic to make clearer responsibilities
3, the concept of Aspect Oriented Programming: in the original longitudinal program execution flow, adding notification for one or a number of methods, cross-section forming process is called Oriented Programming.
4, common concept
1) of the original function: cut-off point, the pointcut
2) pre-notification: before the tangent point functions performed .beforeadvice
. 3) rear notice: the function is performed after the tangent point, afteradvice
. 4) If an exception occurs during the execution of the cut point, will trigger an exception notification .throwsadvice
. 5) section : all functions collectively. (Pointcut pointcut notification and binding of the advice )
6) woven into: embedding section to the original function is called during weaving
5, spring provides two implementations AOP
1) Schema-based
Each notification needs to implement the interface or based
configuration spring configuration file <aop: config> configuration
2) AspectJ
each notification does not need to implement the interface class or
sub-label: spring configuration profile was <config aop> <aop: aspect > configure
Two. Schema-based realization
(A) before, after notification (Schema-based mode)
1, into the jar
2, a new notification class
1) Pre-notification New Class
arg0: cutting point of the object method of Method objects
arg1: cut-point method parameters
arg2: tangent point in which object
public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("执行前置通知"); } }
2) New post notification class
arg0: cutting point method returns a value
arg1: cut-point method of an object
arg2: Method tangent point parameter
object class where the cut-point method: arg3
public class MyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("执行后置通知"); } }
3) configuration profiles spring
incorporated aop namespace
configuration notification class <bean>
configuration section
wildcards match any method names, class names arbitrarily, any one package name
if desired match any process parameters (..)
<?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"> <!-- 配置通知类对象,在切面中引入 --> <bean id="mybefore" class="com.su.advice.MyBeforeAdvice"></bean> <bean id="myafter" class="com.su.advice.MyAfterAdvice"></bean> <!-- 配置切面 --> <aop:config> <!-- 配置切点 --> <aop:pointcut expression="execution(* com.su.test.Demo.demo2())" id="mypoint"/> <!-- 通知 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/> </aop:config> <!-- 配置 Demo 类,测试使用 --> <bean id="demo" class="com.su.test.Demo"></bean> </beans>
4) writing test code
public class Test { public static void main(String[] args) { //原始方法 // Demo demo = new Demo(); // demo.demo1(); // demo.demo2(); // demo.demo3(); ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Demo demo = ac.getBean("demo",Demo.class); demo.demo1(); demo.demo2(); demo.demo3(); } }
5) operating results
demo1
perform pre-notification
demo2
implementing a post notice
demo3
(B) abnormality notification (Schema-based mode)
1, a new class that implements the interface throwsAdvice
1) must write their own method, and must be called afterThrowing
2) There are two ways parameter
must be a 4 or
3) the type of exception to be consistent with the type of exception points at the cut
public class MyThrow implements ThrowsAdvice{ // public void afterThrowing(Method m, Object[] args, Object target, Exception ex) { // System.out.println("执行异常通知"); // } public void afterThrowing(Exception ex) throws Throwable { System.out.println("执行异常通过-schema-base 方式 "); } }
2, the configuration ApplicationContext.xml
<bean id="mythrow" class="com.su.advice.MyThrow"></bean> <aop:config> <aop:pointcut expression="execution(* com.su.test.Demo.demo1())" id="mypoint"/> <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" /> </aop:config> <bean id="demo" class="com.su.test.Demo"></bean>
(C) surrounding the notification (Schema-based mode)
1, the front and rear notifications inform a written notification, notification surround composition
2, to achieve step
1) create a new class that implements MethodInterceptor
public class MyArround the implements MethodInterceptor { @Override public Object Invoke (the MethodInvocation the arg0) throws the Throwable { System.out.println ( "Surround - Front" ); Object Result = arg0.proceed (); // release, call cut-point manner System .out.println ( "surround - rear" ); return Result; } }
2) Configuration applicationContext.xml
<bean id="myarround" class="com.su.advice.MyArround"></bean> <aop:config> <aop:pointcut expression="execution(* com.su.test.Demo.demo1())" id="mypoint"/> <aop:advisor advice-ref="myarround" pointcut-ref="mypoint" /> </aop:config> <bean id="demo" class="com.su.test.Demo"></bean>
Three. AspectJ achieve
(A) front, rear, around the notification (the AspectJ mode)
1, a new class, do not realize
1) the name of any class method
public class MyAdvice { public void mybefore(String name1,int age1){ System.out.println("前置"+name1 ); } public void mybefore1(String name1){ System.out.println("前置:"+name1); } public void myaftering(){ System.out.println("后置 2"); } public void myafter(){ System.out.println("后置 1"); } public void mythrow(){ System.out.println ( "Exception" ); } public Object myarround (ProceedingJoinPoint P) throws the Throwable { System.out.println ( "Surround execution" ); System.out.println ( "Surround - Front" ); Object Result = p.proceed (); System.out.println ( "surround rear" ); return Result; } }
2) Configuration spring configuration file
<aop: after /> post notice, whether abnormal perform
<aop: after-returing /> post notification, performed only when the cut-off point correctly executed
<aop: after /> and <aop : after-returing /> and <aop: after-throwing /> execution order and arrangement order of about
execution () parenthesis can not be spread on args
intermediate use and can not be used && by the spring to and parsed into &&
args (name) custom name the order and the demo1 (parameter, parameter) corresponding to the
<aop: before /> arg- names = " name" name comes from the expression = "" in args (), the name must be the same
args () there are several parameters, arg-names which several parameters must
arg-names = "" name must be inside and the notification method corresponding to the parameter name
<?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"> <bean id="demo" class="com.su.test.Demo"></bean> <bean id="myadvice" class="com.su.advice.MyAdvice"></bean> <aop:config> <aop:aspect ref="myadvice"> <aop:pointcut expression="execution(* com.su.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/> <aop:pointcut expression="execution(* com.su.test.Demo.demo1(String)) and args(name1)" id="mypoint1"/> <aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name1,age1"/> <aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name1"/> <!-- <aop:after method="myafter" pointcut-ref="mypoint"/> <aop:after-returning method="myaftering" pointcut-ref="mypoint"/> <aop:after-throwing method="mythrow" pointcut-ref="mypoint"/> <aop:around method="myarround" pointcut-ref="mypoint"/>--> </aop:aspect> </aop:config> </beans>
(B) exception notification (AspectJ way)
1, reported abnormal only when the cut-off point to trigger an exception notice
2, AspectJ approach provides notification of abnormal way in the spring
3, implementation steps:
1) new Class A, write the name of any method
public class MyThrowAdvice { public void MyException (Exception E1 ) { System.out.println ( "abnormality notification" + e1.getMessage ()); } }
2) arranged in the spring configuration file
<aop: aspect> of ref attribute indicates: Method in which the class.
<AOP: xxxx /> What does it notify
method: When the trigger of this notice, call which method
throwing: abnormal object name, and must notify the same method parameter names (declared exception object may not notice)
<bean id="mythrow" class="com.su.advice.MyThrowAdvice"></bean> <aop:config> <aop:aspect ref="mythrow"> <aop:pointcut expression="execution(* com.su.test.Demo.demo1())" id="mypoint"/> <aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e1"/> </aop:aspect> </aop:config> <bean id="demo" class="com.su.test.Demo"></bean>
IV. Use annotations (based Aspect)
1, does not automatically spring to find the notes, which might have to tell spring annotated classes under the package
1) introduced xmlns: context
<context:component-scan base-package="com.su.advice"></context:component-scan>
2, the Component @
. 1) corresponds to <the bean />
2) If there is no argument, the first letter lowercase the class name, corresponding to <the bean ID = "" />
3) @Component ( "custom name")
3, to achieve step:
1) in the spring profile which is provided in the package annotations
<context:component-scan base-package="com.su.advice,com.su.test"></context:component-scan>
2) adding the class Demo @Componet
added @Pointcut on process ( "") is defined tangent point
@Component public class Demo { @Pointcut("execution(* com.su.test.Demo.demo1())") public void demo1() throws Exception{ // int i = 5/0; System.out.println("demo1"); } }
3) arranged in the notification class
@Component spring class is managed
corresponding to @Aspect <aop: aspect /> indicates the current notification method in the class
@Component @Aspect public class MyAdvice { @Before("com.su.test.Demo.demo1()") public void mybefore(){ System.out.println("前置"); } @After("com.su.test.Demo.demo1()") public void myafter(){ System.out.println("后置通知"); } @AfterThrowing("com.su.test.Demo.demo1()") public void mythrow(){ System.out.println("异常通知"); } @Around("com.su.test.Demo.demo1()") public Object myarround(ProceedingJoinPoint p) throws Throwable{ System.out.println("环绕-前置"); Object result = p.proceed(); System.out.println("环绕-后置"); return result; } }