AOP-aspectJ implements pre-notification, post-notification, surround notification, and parameter transfer

applicationContext.xml

		<aop:config>
	        <aop:aspect ref="myadvice">
	        	<aop:pointcut id="mypoint" expression="execution(* cn.wit.test.Demo.Demo1())" />
	        	<aop:before method="mybefore" pointcut-ref="mypoint"/>
	        	<aop:after method="myafter" pointcut-ref="mypoint"/>
	        	<aop:after-returning method="afterreturn" pointcut-ref="mypoint"/>
	        	<aop:around method="myarround" pointcut-ref="mypoint"/>
	        </aop:aspect>
        </aop:config>
        
        <bean id="demo" class="cn.wit.test.Demo"></bean>

myadvice

  • The difference between the after tag and the afterreturning tag is that the after notification will be executed if the after is abnormal.
  • The method corresponding to the afterreturning tag will not be executed if there is an exception
public class MyAdvice {
    
    
	public void mybefore(){
    
    
		System.out.println("前置");
	}
	public void myafter(){
    
    
		System.out.println("后置");
	}
	public void  afterreturn(){
    
    
		System.out.println("后置returning");
	}
	public Object  myarround(ProceedingJoinPoint p) throws Throwable{
    
    
		System.out.println("环绕-前置");
		Object result = p.proceed();
		System.out.println("环绕-后置");
		return result;
	}
}

test

Insert picture description here

Transfer parameters

  • Compared with schema-based, aspectJ is a lot more troublesome to transfer parameters
  • There is a pre-notification below, the demo1 method has two parameters: int and string

applicationContext.xml

  • The parameters in args (int id1, String name1) come from the pointcut method (int id, String name), and the number, type and order of the parameters must be consistent
  • The parameter after args, the parameter type and parameter name of arg-names (int id1, String name1) must be consistent
  • The parameters in advice (int id2, String name2) must be consistent with the number, type and order of arg-names parameters
<bean id="myadvice" class="cn.wit.advice.MyAdvice"></bean>
        
        <aop:config>
	        <aop:aspect ref="myadvice">
	        	<aop:pointcut id="mypoint" expression="execution(* cn.wit.test.Demo.Demo1(int,String)) and args(id1,name1)" />
	        	<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="id1,name1"/>
	        </aop:aspect>
        </aop:config>
        
        <bean id="demo" class="cn.wit.test.Demo"></bean>

myadvice

public class MyAdvice {
    
    
	public void mybefore(int id2,String name2){
    
    
		System.out.println("前置"+id2+name2);
	}
}


test

public class Test {
    
    
	public static void main(String[] args) {
    
    
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		Demo demo = ac.getBean("demo",Demo.class);
		try {
    
    
			demo.Demo1(1,"张三");
		} catch (Exception e) {
    
    
		} 
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/WA_MC/article/details/112568523