• Spring之aop


    一、关于aop

    1、什么是aop

    AOP即面向切面编程

    2、AOP带来的好处

    让我们可以 “专心做事”
    案例:
    public void doSameBusiness (long lParam,String sParam){
    // 记录日志
    log.info(“调用 doSameBusiness方法,参数是:”+lParam);
    // 输入合法性验证
    if (lParam<=0){
    throws new IllegalArgumentException(“xx应该大于0”);
    }
    if (sParam==null || sParam.trim().equals(“”)){
    throws new IllegalArgumentException(“xx不能为空”);
    }
    // 异常处理
    try{ …
    }catch(…){
    }catch(…){
    }
    // 事务控制
    tx.commit();
    }

    3、AOP中关键性概念

    ①连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.
    ②目标(Target):被通知(被代理)的对象
    完成具体的业务逻辑
    ③通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
    完成切面编程
    ④代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
    例子:外科医生+护士
    只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
    ⑤切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
    (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
    ⑥适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)

    二、Aop的关键名词

    1、前置通知

    ①spring-context.xml配置

    
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    	
    	<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    		
    		<property name="target" ref="bookBiz">property>
    		
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.zking.aop.biz.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    			list>
    		property>
    	bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ②MyMethodBeforeAdvice

    package com.zking.aop.advice;
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    /**
     * 前置通知
     *	买书、评论前加系统日志
     * @author Administrator
     *
     */
    public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
    
    	@Override
    	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    //		目标对象的类名
    		String clzName = arg2.getClass().getName();
    //		当前调用的方法是
    		String methodName = arg0.getName();
    //		当前调用方法所传递参数
    		String args = Arrays.toString(arg1);
    		System.out.println("【系统日志】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args);
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    ③BookBiz

    package com.zking.aop.biz;
    
    public interface BookBiz {
    	// 购书
    	public boolean buy(String userName, String bookName, Double price);
    
    	// 发表书评
    	public void comment(String userName, String comments);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ④BookBizImpl

    package com.zking.aop.biz.impl;
    
    import com.zking.aop.biz.BookBiz;
    import com.zking.aop.exception.PriceException;
    
    public class BookBizImpl implements BookBiz {
    
    	public BookBizImpl() {
    		super();
    	}
    
    	public boolean buy(String userName, String bookName, Double price) {
    		// 通过控制台的输出方式模拟购书
    		if (null == price || price <= 0) {
    			throw new PriceException("book price exception");
    		}
    //		logDao.add->sout("买书相关日志...")
    		System.out.println(userName + " buy " + bookName + ", spend " + price);
    		return true;
    	}
    
    	public void comment(String userName, String comments) {
    		// 通过控制台的输出方式模拟发表书评
    		System.out.println(userName + " say:" + comments);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    ⑤PriceException

    package com.zking.aop.exception;
    
    public class PriceException extends RuntimeException {
    
    	public PriceException() {
    		super();
    	}
    
    	public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    		super(message, cause, enableSuppression, writableStackTrace);
    	}
    
    	public PriceException(String message, Throwable cause) {
    		super(message, cause);
    	}
    
    	public PriceException(String message) {
    		super(message);
    	}
    
    	public PriceException(Throwable cause) {
    		super(cause);
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    ⑥Demo1

    package com.zking.aop.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zking.aop.biz.BookBiz;
    
    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    //		BookBiz bean = (BookBiz)context.getBean("bookBiz");
    		BookBiz bean = (BookBiz)context.getBean("bookProxy");
    		bean.buy("nx", "jpm", 6.6d);
    		bean.comment("sfd", "asd");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    打印结果:
    在这里插入图片描述

    2、后置通知

    ①spring-context.xml配置

    
    <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    	
    	<bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz">bean>
    	
    	
    	<bean class="com.zking.web.UserAction" id="userAction">
    		<property name="userBiz" ref="userBiz">property>
    		<property name="age" value="22">property>
    		<property name="name" value="zhangsan">property>
    		<property name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		property>
    	bean>
    	
    	<bean class="com.zking.web.OrderAction" id="orderAction">
    		<property name="userBiz" ref="userBiz">property>
    		<constructor-arg name="name" value="zhangsan">constructor-arg>
    		<constructor-arg name="age" value="22">constructor-arg>
    		<constructor-arg name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		constructor-arg>
    	bean>
    	
    	
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    	
    	<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    	
    	<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    		
    		<property name="target" ref="bookBiz">property>
    		
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.zking.aop.biz.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    				<value>myAftervalue>
    			list>
    		property>
    	bean>
    	
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    ②Demo1

    package com.zking.aop.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zking.aop.biz.BookBiz;
    
    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    //		BookBiz bean = (BookBiz)context.getBean("bookBiz");
    		BookBiz bean = (BookBiz)context.getBean("bookProxy");
    		bean.buy("nx", "jpm", 6.6d);
    		bean.comment("sfd", "asd");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ③MyAfterReturningAdvice

    package com.zking.aop.advice;
    
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    public class MyAfterReturningAdvice implements AfterReturningAdvice{
    
    	@Override
    	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
    //		目标对象的类名
    		String clzName = arg3.getClass().getName();
    //		当前调用的方法是
    		String methodName = arg1.getName();
    //		当前调用方法所传递参数
    		String args = Arrays.toString(arg2);
    		System.out.println("【买书返利】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args+";目标对象方法返回值为:"+arg0);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    打印结果:
    在这里插入图片描述

    3、环绕通知

    ①Demo1

    package com.zking.aop.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zking.aop.biz.BookBiz;
    
    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    //		BookBiz bean = (BookBiz)context.getBean("bookBiz");
    		BookBiz bean = (BookBiz)context.getBean("bookProxy");
    		bean.buy("nx", "jpm", 6.6d);
    		bean.comment("sfd", "asd");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ②MethodInterceptor

    package com.zking.aop.advice;
    
    import java.util.Arrays;
    
    import org.aopalliance.intercept.MethodInvocation;
    
    /**
     * 环绕通知=前置通知+后置通知
     * @author Administrator
     *
     */
    public class MethodInterceptor implements org.aopalliance.intercept.MethodInterceptor{
    
    	@Override
    	public Object invoke(MethodInvocation arg0) throws Throwable {
    //		目标对象的类名
    		String clzName = arg0.getThis().getClass().getName();
    //		当前调用的方法是
    		String methodName = arg0.getMethod().getName();
    //		当前调用方法所传递参数
    		String args = Arrays.toString(arg0.getArguments());
    		System.out.println("【环绕通知】:"+clzName+"."+methodName+"被调用,传递的参数为:"+args);
    //		方法的返回值	执行目标方法	bookBiz.buy(as,jasd,6.6);
    		Object rs = arg0.proceed();
    		System.out.println("【环绕通知】:目标对象方法返回值为:"+rs);
    		return rs;
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    ③spring-context.xml

    
    <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    	
    	<bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz">bean>
    	
    	
    	<bean class="com.zking.web.UserAction" id="userAction">
    		<property name="userBiz" ref="userBiz">property>
    		<property name="age" value="22">property>
    		<property name="name" value="zhangsan">property>
    		<property name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		property>
    	bean>
    	
    	<bean class="com.zking.web.OrderAction" id="orderAction">
    		<property name="userBiz" ref="userBiz">property>
    		<constructor-arg name="name" value="zhangsan">constructor-arg>
    		<constructor-arg name="age" value="22">constructor-arg>
    		<constructor-arg name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		constructor-arg>
    	bean>
    	
    	
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    	
    	<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    	
    	<bean class="com.zking.aop.advice.MethodInterceptor" id="myMethod">bean>
    	
    	<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    		
    		<property name="target" ref="bookBiz">property>
    		
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.zking.aop.biz.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    				<value>myAftervalue>
    				<value>myMethodvalue>
    			list>
    		property>
    	bean>
    	
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    打印结果:
    在这里插入图片描述

    4、异常通知

    ①Demo1

    package com.zking.aop.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zking.aop.biz.BookBiz;
    
    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    //		BookBiz bean = (BookBiz)context.getBean("bookBiz");
    		BookBiz bean = (BookBiz)context.getBean("bookProxy");
    		bean.buy("nx", "jpm", -6.6d);
    		bean.comment("sfd", "asd");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ②MyThrowsAdvice

    package com.zking.aop.advice;
    
    import org.springframework.aop.ThrowsAdvice;
    
    import com.zking.aop.exception.PriceException;
    
    /**
     * 关于过滤通知
     * 	相较于前置通知、后置通知、环绕通知有一个非常大的区别
     * 	前面三大通知都需要实现其中的方法
     * 	环绕通知则不需要,但是,它的方法名又是固定的;
     * @author Administrator
     *
     */
    public class MyThrowsAdvice implements ThrowsAdvice{
    	public void after(PriceException p) {
    		System.out.println("【异常通知】:当价格发生异常,那么执行此处代码块!!!");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ③spring-context.xml

    
    <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    	
    	<bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz">bean>
    	
    	
    	<bean class="com.zking.web.UserAction" id="userAction">
    		<property name="userBiz" ref="userBiz">property>
    		<property name="age" value="22">property>
    		<property name="name" value="zhangsan">property>
    		<property name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		property>
    	bean>
    	
    	<bean class="com.zking.web.OrderAction" id="orderAction">
    		<property name="userBiz" ref="userBiz">property>
    		<constructor-arg name="name" value="zhangsan">constructor-arg>
    		<constructor-arg name="age" value="22">constructor-arg>
    		<constructor-arg name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		constructor-arg>
    	bean>
    	
    	
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    	
    	<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    	
    	<bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    	
    	<bean class="com.zking.aop.advice.MethodInterceptor" id="myMethod">bean>
    	
    	<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    		
    		<property name="target" ref="bookBiz">property>
    		
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.zking.aop.biz.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    				<value>myAftervalue>
    				<value>myMethodvalue>
    				<value>myThrowsvalue>
    			list>
    		property>
    	bean>
    	
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    运行如下:
    在这里插入图片描述

    5、过滤通知

    ①spring-context.xml

    
    <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    	
    	<bean class="com.zking.biz.impl.UserBizImpl1" id="userBiz">bean>
    	
    	
    	<bean class="com.zking.web.UserAction" id="userAction">
    		<property name="userBiz" ref="userBiz">property>
    		<property name="age" value="22">property>
    		<property name="name" value="zhangsan">property>
    		<property name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		property>
    	bean>
    	
    	<bean class="com.zking.web.OrderAction" id="orderAction">
    		<property name="userBiz" ref="userBiz">property>
    		<constructor-arg name="name" value="zhangsan">constructor-arg>
    		<constructor-arg name="age" value="22">constructor-arg>
    		<constructor-arg name="hobby">
    			<list>
    				<value>篮球value>
    				<value>boyvalue>
    				<value>篮球value>
    			list>
    		constructor-arg>
    	bean>
    	
    	
    	<bean class="com.zking.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    	
    	<bean class="com.zking.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    	
    	<bean class="com.zking.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    	<bean>bean>
    	
    	<bean class="com.zking.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    	
    	<bean class="com.zking.aop.advice.MethodInterceptor" id="myMethod">bean>
    	
    	<bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfterPlus">
    		<property name="advice" ref="myAfter">property>
    		<property name="pattern" value=".*buy">property>
    	bean>
    	
    	<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    		
    		<property name="target" ref="bookBiz">property>
    		
    		<property name="proxyInterfaces">
    			<list>
    				<value>com.zking.aop.biz.BookBizvalue>
    			list>
    		property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>myBeforevalue>
    				
    				<value>myAfterPlusvalue>
    				<value>myMethodvalue>
    				<value>myThrowsvalue>
    			list>
    		property>
    	bean>
    	
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    ②Demo1

    package com.zking.aop.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.zking.aop.biz.BookBiz;
    
    public class Demo1 {
    	@SuppressWarnings("resource")
    	public static void main(String[] args) {
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
    //		BookBiz bean = (BookBiz)context.getBean("bookBiz");
    		BookBiz bean = (BookBiz)context.getBean("bookProxy");
    		bean.buy("nx", "jpm", 6.6d);
    		bean.comment("sfd", "asd");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    netty系列之:netty中常用的字符串编码解码器
    QT Day5思维导图
    国庆第五天
    v-cloak指令的使用
    ArcGIS Pro脚本工具(6)——修复CAD图层数据源
    【GPU】Nvidia CUDA 编程高级教程——利用蒙特卡罗法求解 的近似值
    11.18-创建视图 11.19-SELECT语法结构 11.20-简单查询 11.21-连接查询 11.22-聚集函数 11.23-授权语句
    【LeetCode】1687. 从仓库到码头运输箱子
    AC修炼计划(AtCoder Regular Contest 162)
    SpringCloud-5.服务配置(SpringCloud Config)
  • 原文地址:https://blog.csdn.net/weixin_67677668/article/details/126212611