spring Learning Notes - --- AOP

Keywords: Spring xml

AOP advantage:

* During the operation of the program, the existing methods are enhanced without modifying the source code; in the actual business development process, the business logic parts can be isolated, which reduces the coupling degree of the business logic parts, improves the reusability of the program, and improves the development efficiency.

Related terms in AOP:

* Joint Point (Connection Point) - - Popular explanation: Business layer methods can be called Connection Points;

* PointCut - Definition of those methods that intercept JointPoint; (i.e. enhanced methods);

* Advice - Notification is the only thing you need to do to intercept JointPoint.

Pre-notification, post-notification, exception notification, final notification, circular notification;

* Introduction - No discussion for the time being.

* Target - AccountService;

* Weaving - The process of applying enhancements to target objects to create new proxy objects;

Spring - dynamic proxy weaving - AspectJ - compile time + load class weaving

* proxy (proxy) - - the proxy object generated by the target object;

* Aspect - PointCut and advice or introduction;

XML configuration implements AOP:

* Introduce relevant jar packages as constraints, configure spring bean proxy classes (services) and their own defined notification classes (loggers)

//Configuration requires proxy entity classes
<Bean id="accountService" class="com.spring.demo.impl.AccountServiceImpl">

//Configuration log class
<Bean id="logger" class="com.spring.demo.util.Logger">

* Open the AOP configuration interior to configure the notification type with the corresponding label

Note: The pointcut expression execution (expression)

Expression: Access modifier return value type package name, package name, package name, class name, method name (parameter list)

      e.g. public void com.spring.demo.impl.AccountServiceImpl.transfer(int i)

Global wildcards can be used to denote *. *. *. (.)

* com.spring.demo.impl. *. * (.) in the actual business development process

//Open aop configuration
<aop:config>
    //Configure the section id to provide the section with a bean id that uniquely identifies the ref specified notification class
    <aop:aspect id="logAdvice" ref="logger">
        //Internally configure notification types with corresponding labels
        // Method is used to specify what method is in the notification class
        //Point Cut is used to specify pointCut expressions  
        //Before advice
        <aop:before method="beforPrintLog()" pointCut="excution(* com.spring.demo.impl.*.*(..))">
        //after returning advise
        <aop:after-returning method="afterReturnPrintLog()" pointCut="excution(* com.spring.demo.impl.*.*(..))">
        //Exception notification
        <aop:after-throwing method="afterThrowingPrintLog()" pointCut="excution(* com.spring.demo.impl.*.*(..))">
        //Final notice
        <aop:after method="AfterprintLog()" pointCut="excution(* com.spring.demo.impl.*.*(..))">

    </aop:aspect>
</aop:config>

* Configurable pointcut expressions simplify configuring pointcut expressions for each of the above notifications

//Open aop configuration
<aop:config>
    //Configure the section id to provide the section with a bean id that uniquely identifies the ref specified notification class
       
    //Configure the pointcut expression id to specify the expression pointcut expression that is unique to the expression
    <aop:pointCut id="pl" expression="excution(* com.spring.demo.impl.*.*(..))">

    <aop:aspect id="logAdvice" ref="logger">
        //Internally configure notification types with corresponding labels
        // Method is used to specify what method is in the notification class
        //Point Cut is used to specify pointCut expressions  
        //Before advice
        <aop:before method="beforPrintLog()" pointCut-ref="pl">
        //after returning advise
        <aop:after-returning method="afterReturnPrintLog()" pointCut-ref="pl">
        //Exception notification
        <aop:after-throwing method="afterThrowingPrintLog()" pointCut-ref="pl">
        //Final notice
        <aop:after method="AfterprintLog()" pointCut-ref="pl">

    </aop:aspect>
</aop:config>

      

* Configuration surround notification:

//configuration file
<aop:config>
    
    //Configure the pointcut expression id to specify the expression pointcut expression that is unique to the expression
    <aop:pointCut id="pl" expression="execution(* com.spring.demo.impl.*.*(..))">

    <aop:aspect id="logAdvice" ref="logger">
       
        //Around Advice
        <aop:around method="aroundprintLog()" pointCut-ref="pl">

    </aop:aspect>
</aop:config>


* Writing a surround notification method

/**
* Spring The surround notification provides us with a way to manually control the proper execution of enhancements in code
*/
public void aroundPrintLog(ProceedinJoinPoint pjp){
	Object value = null;
	try{
		Object[] args = pjp.getArgs(); //Get the parameters needed for method execution
		
		System.out.println("Logger in around Method to start logging---------Preposition");
		
		value = pjp.proceed(args);  //Clearly invoke business layer methods (entry point methods)
		
		System.out.println("Logger in around Method to start logging--------Postposition");
            return value;
	}catch(Throwable t){
	
		System.out.println("Logger in around Method to start logging--------abnormal");
	
	}finally{
		System.out.println("Logger in around Method to start logging--------Final");
	}

}

Implementation of AOP by Annotation

//Open Annotation Scanning to Create Our Objects with spring
<context:component-scan base-package="com.spring.demo"></<context:component-scan>

//Configure spring to open AOP support for annotations
<context:aspectj-autoproxy></context:aspectj-autoproxy>
@Component("logger")
@Aspect  //Represents that the current class is a faceted class
public class Logger(){

	@Pointcut("execution(* com.spring.demo.impl.*.*(..))")
	private void pl(){}
	
	/**
	* Before advice
	*/
	@Before("pl()")
	public void beforePrintLog(){System.out.println("Before advice Logger")};

	/**
	* after returning advise
	*/
	@AfterReturning("pl()") 
	public void afterReturningPrintLog(){System.out.println("after returning advise Logger")};
	
	/**
	* Exception notification
	*/
	@AfterThrowing("pl()")
	public void afterThrowingPrintLog(){System.out.println("Exception notification Logger")};
	
	/**
	* Final notice
	*/
	@After("pl()")
	public void afterPrintLog(){System.out.println("Final notice Logger")};




}

Note: When spring uses annotations to do aop, the method invocation sequence will be problematic, that is, the final notification is executed before the latter, resulting in the failure to report errors and the unsuccessful execution.

But when surround notification is replaced, it is executed sequentially.

Therefore, in general, in actual development, it is still their own definition of surround notifications, and then use annotations to achieve. The program runs perfectly.

Posted by LeeReed on Sat, 11 May 2019 12:44:44 -0700