IOC and AOP understanding (continually updated)

IOC: Inversion of Control, in fact, its essence is to create the right to use the bean container to control the ioc, ioc bean responsible for the creation and dependency injection, in essence, it creates a personal feel that is Java's reflection mechanism to generate a bean counter object. So why ioc want to do it? In fact, in order to reduce decoupling between the program code, it can be said to reduce the dependencies between individual programs. In fact, ioc like a big factory, what you want to instance of an object you can find ioc container take, naturally decoupling is greatly reduced. As for how to get it to be handled by the configuration XML file, of course, this configuration is certainly greatly reduced efficiency, so spring has launched annotations to reduce write xml configuration, really good use.

AOP: Aspect Oriented Programming name is very vivid, the section of the code, cut inside and can fill something new. In fact, it seems to me, aop by matching method, add new features based on the original methods, which play a good role in the safety of the handler. Can be filtered in advance at the time of treatment, you can add data processing method for obtaining parameters when the data better maintenance and so on. In fact, its essence is to the original method already written to add new features. And indeed a good thing.

Note: But add annotations or XML configuration, the compiler will be executed, but like annotations and XML are equipped to achieve, then the compiler will be fully implemented, the equivalent to twice.

AOP: 

Enhanced five ways: enhanced front, rear enhancement, enhanced surround (similar to the try), enhanced exception is thrown (corresponding to the catch), ultimately enhancing (corresponding to finally)

Notes realization ( pre- enhancement):

 It must be declared:    if there is no declaration of the notes are invalid

XML configuration is equivalent to:

 

 

Notes realization ( post- enhancement):

 

 

Equivalent to the XML configuration (above the rest):

Note: Since the return value is set as the target method will be enhanced returning the attribute value, when the target process (entry point) when the parameter is not specified, is null.

 

Annotations implement ( surround enhancement): the target method may be controlled by the method of execution proceed

 

 

 Equivalent to the XML configuration (above the rest):

 

Notes achieve ( an exception is thrown enhancement):

Equivalent to the XML configuration (above the rest):

 

 

Notes realization ( final enhancement):

 

 

  Equivalent to the XML configuration (above the rest):

 

 

 

Finally, the order of their execution (no exceptions):

 

Abnormal: as the front surround exception code order of the catch thus surrounded directly to seize all want to perform abnormality is present then this can be removed in a surround try ... catch be implemented

 

 

 A little early on, we can draw some conclusions:  

  1. Notes and writing basic XML is similar annotation is used to help us be more efficient to write code, XML-based configuration annotations are configured to help us.

  2. After performing the surround enhancement control tangent point, after it performs a final enhancement performed, and then after performing post enhanced, when there is an abnormality rear tangent point of the reinforcement will not be executed, of course, performed around the cut point the code is not executed. As shown below:

    2.1: Normal processing

      

    2.2: Exception Handling

      

 

 

 Note: In addition to the method Around annotation can pass ProceedingJionPoint, the other a few can not pass this class. But ordinary type of data is not restricted. Annotated method's return value is not limited, and restrictions on freedom.

 

 In aop in check not by how to keep the program into the core code?

1. @Before: This comment will not be executed only when abnormal cut-off point. Normal @Before not stop the current thread into the connection point.

Exception:

2. @Around: we generally use this to verify, since it is encapsulated by the method ProceedingJionPoint proxy class proceed () to control the execution timing of the point of tangency.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ashen137/p/11808765.html