Aop (plane-oriented programming)

Source: Internet
Author: User


Aop (plane-oriented programming)



AOP Benefits



1, each business logic in one place (transaction management, log management, security, other), to avoid the spread of code to all corners



2. Clearer business logic



AOP terminology



1, cut-off: need to achieve cross-function



2. Connection point: The place where the slice can be inserted during the execution of the application system (call of method, modify field, throw exception)



3. Notification: The specific implementation of the notification slice, which informs the application of the new behavior of the system. Notifications are inserted into the application system at the connection point



4. Entry point: The actual inserted connection point, usually by the development of the class name and method name or match the class name and method of the regular expression of the style to specify the Pointcut



5. Introduction: Adding new properties and methods to the class



6. Target object: The object to be notified



7. Agent: The object that is created after the notification is applied to the target object.



8. Weaving: The process of applying facets to the target object to create a new proxy object.



Compile time: Java-------class file



Class Load period: Class loader------JVM



Run-time: facets are woven into the application runtime (typically, the AOP container dynamically generates a proxy object for the delegate target object when the facet is woven)



The AOP implementation of spring



Key points:



1. Write spring notifications in Java



2. Spring's runtime Notification object



3. Spring Implements AOP Federated interface



4. Spring only supports method connection points



Create a notification



1. Front-facing notification



Implement Methodbeforeadvice



public class Mybeforeadvice implements methodbeforeadvice{



@Override



Publicvoid before (method, object[] args, Object target)



throwsthrowable {



}



}



Configure Bean.xml



<!--Configuring a pre-notification-



<bean id= "Mybeforeadvice" class= "Cn.csdn.hr.advice.MyBeforeAdvice"/>



<!--Introducing Notifications-



<bean id= "Auditablebean" class= "Cn.csdn.hr.service.AuditableBean"/>



<bean id= "Personservicebean" class= "Cn.csdn.hr.service.PersonServiceBean"/>



<bean id= "Proxyfactorybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" >



<!--Agent interface---



<property name= "Proxyinterfaces" >



<list>



<value>cn.csdn.hr.service.PersonService</value>



<value>cn.csdn.hr.service.Auditable</value>



</list>



</property>



<!--notification NAME--



<property name= "Interceptornames" >



<list>



<value>myBeforeAdvice</value>



</list>



</property>



<!--notification objects--



<property name= "Target" >



<ref bean= "Personservicebean"/>



</property>



</bean>



2. Rear-facing notification



Implement Afterreturningadvice



Publicclass Myafteradivce implements afterreturningadvice{



@Override



publicvoid afterreturning (Object returnvalue, method,



Object[] args, Object target) throws Throwable {



}



}



Configure Beans.xml



<!--configuring post notifications-



<bean id= "Myafteradivce" class= "Cn.csdn.hr.advice.MyAfterAdivce"/>



<!--Introducing Notifications-



<bean id= "Auditablebean" class= "Cn.csdn.hr.service.AuditableBean"/>



<bean id= "Personservicebean" class= "Cn.csdn.hr.service.PersonServiceBean"/>



<bean id= "Proxyfactorybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" >



<!--Agent interface---



<property name= "Proxyinterfaces" >



<list>



<value>cn.csdn.hr.service.PersonService</value>



<value>cn.csdn.hr.service.Auditable</value>



</list>



</property>



<!--notification NAME--



<property name= "Interceptornames" >



<list>



<value>myBeforeAdvice</value>



</list>



</property>



<!--notification objects--



<property name= "Target" >



<ref bean= "Personservicebean"/>



</property>



</bean>



3. Surround Notification



Publicclass Myaroundadvice implements Methodinterceptor {



@Override



Public Object Invoke (Methodinvocation methodinvocation) throws throwable{



}



}



Configure Beans.xml



<!--surround Notifications-



<bean id= "Myaroundadvice" class= "Cn.csdn.hr.advice.MyAroundAdvice"/>



<!--Introducing Notifications-



<bean id= "Auditablebean" class= "Cn.csdn.hr.service.AuditableBean"/>



<bean id= "Personservicebean" class= "Cn.csdn.hr.service.PersonServiceBean"/>



<bean id= "Proxyfactorybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" >



<!--Agent interface---



<property name= "Proxyinterfaces" >



<list>



<value>cn.csdn.hr.service.PersonService</value>



<value>cn.csdn.hr.service.Auditable</value>



</list>



</property>



<!--notification NAME--



<property name= "Interceptornames" >



<list>



<value>myBeforeAdvice</value>



</list>



</property>



<!--notification objects--



<property name= "Target" >



<ref bean= "Personservicebean"/>



</property>



</bean>



4. Exception notification



Publicclass Mythrowsadivce implements Throwsadvice {



Exception handling Operations



publicvoid afterthrowing (methodmethod,object[] args, Object target,throwable throwable) {



System.out.println ("Exception----------------------------" +throwable.getmessage ());



}



}



Configure Beans.xml



<!--configuring exception notifications-



<bean id= "Mythrowsadivce" class= "Cn.csdn.hr.advice.MyThrowsAdivce"/>



<!--Introducing Notifications-



<bean id= "Auditablebean" class= "Cn.csdn.hr.service.AuditableBean"/>



<bean id= "Personservicebean" class= "Cn.csdn.hr.service.PersonServiceBean"/>



<bean id= "Proxyfactorybean" class= "Org.springframework.aop.framework.ProxyFactoryBean" >



<!--Agent interface---



<property name= "Proxyinterfaces" >



<list>



<value>cn.csdn.hr.service.PersonService</value>



<value>cn.csdn.hr.service.Auditable</value>



</list>



</property>



<!--notification Name


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.