AOP and its application in Spring

xiaoxiao2021-04-07  346

AOP Introduction OOP thinks have a profound impact on modern programming, but in certain aspects, OOP also has its shortcomings. For example, in terms of logging (log), transaction, etc., Apply OOP will generate a lot of code repetition, although this repetition can be reduced by some design patterns, but we have better Solution, that is, AOP (Aspect Oriented Programming). AOP is a recent rise of programming ideas, which is an OOP thought supplement, not its opposite. AOP, from the literal understanding is the cutting programming, with a relatively popular example, for example, permission check is required before accessing multiple objects, then, if you follow the object-oriented idea, the permission check will be becoming This multiple object behavior. If each object needs to implement these behaviors, it will inevitably cause a large number of duplicate code generation, and the writing program will also be borless. But we can regard permission checks as a cut, all access to these objects. To learn about AOP, you must first understand a few basic and very important concepts. Aspect: The cross section during the object operation. Such as authority checks, logs, transaction processing, etc. Join Point: Some phases in the program run. If a method is called, the abnormality thrown. Advice: The processing logic used by a connection point. PointCut: A collection of connection points, which indicates when Advice is triggered. Or use an example to illustrate an example of all, there is now such a DomainObjDAO interface and its implementation class DomainObjDAOImplDomainObjDAO.java:public interface DomainObjDAO {public void save ();} DomainObjDAOImpl: public class DomainObjDAOImpl implements DomainObjDAO {private Logger logger = Logger.getLogger (this .getClass (). getname ()); public void save () {system.out.println ("Saving Domain Object ...");} Now you need to add a lock to the business object in the Save method. For example, lock and unlock before and after SAVE. Get this demand, in the premise that the external call logic and does not change the existing code change, the Proxy mode (GOF) is a good choice, adding a proxy class to implement the DomainobjDao interface, in its implementation method, DomainobjdaoImpl class Save method and call the Lock and Unlock method before and after Save. This approach makes it easy to change the external call logic and existing code, but if there are multiple Domainobjimpl, the drawback of this method is exposed, we must implement the same proxy class as the Domainobjimpl number to implement this function, this It will be very horrible to us and unacceptable. This example once again confirmed that we have described, for such problems, OOP seems to be a heart, and AOP can solve it well, and the dynamic agent provided by JDK1.3 will use the AOP's thoughts to solve this problem. Provide a good idea, let's see how it is achieved.

Dynamic proxy implementation AOP public class LockHandler implements InvocationHandler {private Logger logger = Logger.getLogger (this.getClass () getName ().); Private Object originalObject; public Object bind (Object obj) {logger.info ( "coming here .. . "); this.originalObject = obj; return Proxy.newProxyInstance (obj.getClass () getClassLoader (), obj.getClass () getInterfaces (), this);..} public Object invoke (Object arg0, Method arg1, Object [] Arg2) throws throwable {Object results = null; if (arg1.getname (). StartSwith ("save")) {lock (); result = arg1.invoke (this.originalObject, arg2); unlock ();} Return Result;} private void Lock () {logger.info ("Lock Object ...");} private void unlock () {logger.info ("unlock object ...");}} There is no in the above code. The interface associated with the specific application layer and the class is referenced, so it is applicable to all classes. This solves the disadvantages of using a common proxy class. However, the dynamic agent requires the class of the agent to be an implementation of an interface (this can be seen by obj.getclass (). GetInterfaces ()), but this also conforms to object-oriented design ideas, if the agent is not implemented Any interface can be implemented by gclib, which is no longer detailed here.

转载请注明原文地址:https://www.9cbs.com/read-132566.html

New Post(0)