Introduction to AOP
AOP (aspectoriented programming, aspect-oriented programming), which can be said to complement and perfect OOP (object-oriented programing, object-oriented programming), is a hotspot in software development, is also an important part of the spring framework. AOP can encapsulate the logic or responsibility that is not related to the business, but is called by the business module, such as transaction processing, log management, permission control, etc., so as to reduce the duplication code of the system, reduce the coupling between modules, and benefit the future operability and maintainability.
Basic concepts of AOP
1. Aspect: Modularization of crosscutting concerns;
2, Advice: The specific implementation of crosscutting concerns, including the call target before execution (before Advice), after the call target execution (after Advice), throw exception (throw Advice);
3, Pointcut: The scope of application of crosscutting concerns, it defines the application of advice to which joinpoint; application scope, the first * indicates that all have no return value, the second * indicates that there are no parameters, representing the match all cases;
4. Joinpoint: The connection point, where the target method is called. "Where to do" is indicated in AOP;
5, Weave: Weaving, the application of advice to the targetobject process;
6, Introduction: Introduced, can dynamically add a method to the class, the enhancement of the class is called introduction (Introduction), the enhancement of the method is called weaving (Weaving).
Implementation Method
This demo implements security checks before calling the method.
first, the way of annotation (annotation mode)
Immutable Code:
<span Style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package com.jmj.spring;/** * Classname:usermanager * @Description: Interface * @author Meng * @date 2016-5-6 morning 10:51:44 */pu Blic interface Usermanager {public void AddUser (string username, string password);p ublic void deluser (int userId);} </span>
<span Style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package com.jmj.spring;/** * Classname:usermanagerimpl * @Description: Specific implementation class * @author Meng * @date 2016-5-6 10:51: */public class Usermanagerimpl implements Usermanager {public void AddUser (string username, string password) {//checkse Curity (); System.out.println ("---------usermanagerimpl.add ()--------");} public void Deluser (int userId) {//checksecurity (); System.out.println ("---------usermanagerimpl.deluser ()--------");}} </span>
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.jmj.spring;import Org.springframework.beans.factory.beanfactory;import org.springframework.context.support.classpathxmlapplicationcontext;/** * classname:client * @Description: Client * @ Author Meng * @date 2016-5-6 morning 10:52:05 */public class Client {public static void main (string[] args) {//Read config file beanfactory Factory =new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Usermanager usermanager= (Usermanager) Factory.getbean ("Usermanager") Usermanager.adduser ("Zhang San", "123");}} </span>
Important part:
1. Add annotations to the Crosscutting class
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.jmj.spring;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.Aspect; Import Org.aspectj.lang.annotation.before;import org.aspectj.lang.annotation.pointcut;//modularity of crosscutting concerns// Using annotations: Annotation@aspectpublic class Securityhandler {//definition check Security/** * Define advice before calling Add method, Represents a specific implementation of advice applied to those pointcut subscribed Joinpoint *///to crosscutting concerns @before ("Addaddmethod ()") //Pre-enhancement//@After ("Addaddmethod () ") //post-enhanced private void checksecurity () {System.out.println ("-------checksecurity-------");} /** * Defines the name of the Pointcut,pointcut as Addaddmethod (), this method does not return a value and the parameter * The method is an identity that does not invoke the application scope of the *///crosscutting concern//Application scope, the first * indicates all have no return value, the second one * Indicates if there are no parameters, which means match all cases @pointcut ("Execution (* add* (..))") private void Addaddmethod () {};} </span>
2. In the configuration file (applicationcontext.xml), enable ASPECTJ support for annotation
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><!--enable ASPECTJ support for annotation- <aop:aspectj-autoproxy/> <bean id= "Usermanager" class= "Com.jmj.spring.UserManagerImpl"/> <bean id= "Securityhandler" class= " Com.jmj.spring.SecurityHandler "/></span>
Second, the configuration file modewith annotations, the Crosscutting class (Securityhandler) needs to be modified as long as the requirements change, and the crosscutting classes are too complex. This type of decompression can be used in the configuration file mode.
1. Cross-cutting class
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public class Securityhandler {private void checksecurity () {System.out.println ("-------checksecurity-------");}} </span>
in this class, we only need to cut into the concrete implementation of the method, and do not need the tube how to cut into?
2, in the configuration file (Applicationcontext.xml), the management of specific cut-in form
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > <bean id= "Usermanager" class= "Com.jmj.spring.UserManagerImpl"/> <bean id= "Securityhandler" class= "C Om.jmj.spring.SecurityHandler "/> <aop:config> <!--equivalent to aspect in annotations, modular for crosscutting concerns-- <aop:aspect id= "Securityaspect" ref= "Securityhandler" > <!--defines which advice the joinpoint is applied to, For spring, method calls-<!--the method that starts with add-<!--<aop:pointcut id= "Addaddmethod" expression= "E Xecution (* add* (..)) " />-<!--can use a reference package, the scope is: com.jmj.spring all of the classes under this package--<!--<aop:pointcut I D= "Addaddmethod" expression= "Execution (* com.jmj.spring.*.* (..))" />-<!--only works with additions and deletions, and works with Add and Del Methods--<aop:pointcut id= "Addaddmethod" ex pression= "Execution (* com.jmj.spring.*.add* (..)) | | Execution (* com.jmj.spring.*.del* (..)) " /> <!--to crossThe specific implementation of the focus point--<aop:before method= "checksecurity" pointcut-ref= "Addaddmethod"/> <!--<AOP : After Method= "checksecurity" pointcut-ref= "Addaddmethod"/>-</aop:aspect> </aop:confi G></span>
Summary
The relationship between AOP and OOP:
Advantages of AOP: Low coupling, high expansion.
"SSH" AOP