Spring5企业级开发实战(七)————SpringAOP实战之增强类型

概念介绍

增强类型

AOP联盟为增强定义了org.aopalliance.aop.Advice接口,SpringAOP按照增强点的连接点位置可以分为5种

  • 前置增强:表示在目标方法之行前增强。
  • 后置增强:表示在目标方法执行后实施增强。
  • 环绕增强: 表示在目标方法之行前后实施增强。
  • 异常抛出增强:表示在目标方法抛出异常之后进行增强。
  • 引介增强:表示在目标类中添加一些新的方法或者属性。

相关的代码

  • 前置增强代码
package com.Spring增强;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Author:haozhixin
 * Func:  SpringAOP之前置增强
 * Date:  20190808
 */
@Component
public class SpringBeforeAdvice implements MethodBeforeAdvice {
	@Override
	public void before(Method method, Object[] objects, Object o) throws Throwable {
		System.out.println("前置增强方法是:"+method.getName());
		System.out.println("前置增强方法的参数是:"+objects);
		System.out.println("前置增强对象是"+o);
	}
}

  • 后置增强的代码
package com.Spring增强;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
* Author:haozhixin
* Func: 后置增强
* Date: 20190808
*/
@Component
public class SpringAfterAdvice implements AfterReturningAdvice {
   @Override
   public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
   	System.out.println("后置增强的方法返回值是:"+o);
   	System.out.println("后置增强的方法是:"+method.getName());
   	System.out.println("后置增强的方法的参数:"+objects[0]);
   	System.out.println("后置增强的对象是:"+o1);
   }
}

  • 环绕增强的代码
package com.Spring增强;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

/**
* Author:haozhixin
* Func:  SpringAOP环绕增强
* Date:  20190808
*/
@Component
public class SpringMethodInterCeptor implements MethodInterceptor {
   @Override
   public Object invoke(MethodInvocation methodInvocation) throws Throwable {
   	System.out.println("前置增强执行了");
   	//通过反射机制调用目标方法
   	Object obj = methodInvocation.proceed();
   	System.out.println("后置增强执行了");
   	return obj;
   }
}

  • 抛出异常增强
package com.Spring增强;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.stereotype.Component;

/**
 * Author:haozhixin
 * Func: 抛出异常增强
 * Date: 20190808
 */
@Component
public class SpringThrowAdvice implements ThrowsAdvice{
	public void afterThrowing(Exception e) throws Throwable{
		System.out.printf("异常抛出增强执行,%s%n",e);
	}
}

  • 引介增强
package com.Spring增强;

/**
 * Author:haozhixin
 * Func:
 * Date:
 */
public interface ManagerMent {
	void manage(String name);
}

package com.Spring增强;

import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * Author:haozhixin
 * Func:
 * Date:
 */
public class Manager extends DelegatingIntroductionInterceptor implements ManagerMent{
	@Override
	public void manage(String name) {
		System.out.println(name+",您好,我是经理,负责管理服务员");
	}
}

  • 被增强的类:
package com.Spring增强;

import org.springframework.stereotype.Component;

/**
 * Author:haozhixin
 * Func:
 * Date:
 */
@Component
public class Waiter {
	public String Serve(String name){
		System.out.println(name+",您好,很高兴为您服务");
		return name+",现在是北京时间XXX";
	}

	public void driving(String name){
		throw new RuntimeException(name+",禁止酒后驾车");
	}
}

  • 单元测试类
import com.Bean声明及调用.IntrduceDemo;
import com.Bean声明及调用.Student;
import com.Bean声明及调用.User;
import com.Spring增强.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Author:haozhixin
 * Func:
 * Date:
 */
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:SpringAdvice.xml")
public class SpringAdviceTest {
/*	@Autowired
	private SpringBeforeAdvice springBeforeAdvice;
	@Autowired
	private SpringAfterAdvice springAfterAdvice;
	@Autowired
	private SpringMethodInterCeptor springMethodInterCeptor;
	@Autowired
	private SpringThrowAdvice springThrowAdvice;
	@Autowired
	private Waiter waiter;
	//@Test
	public void testSpringBeforeAdvice(){
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(waiter);
		proxyFactory.addAdvice(springBeforeAdvice);
		Waiter proxy = (Waiter)proxyFactory.getProxy();
		proxy.Serve("郝志新");
		proxy.Serve("曹快快");
	}
	//@Test
	public void testSpringAfterAdvice(){
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(waiter);
		proxyFactory.addAdvice(springAfterAdvice);
		Waiter proxy = (Waiter)proxyFactory.getProxy();
		proxy.Serve("郝志新");
		proxy.Serve("曹快快");
	}
	//@Test
	public  void testspringMethodInterCeptor(){
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(waiter);
		proxyFactory.addAdvice(springMethodInterCeptor);
		Waiter proxy = (Waiter)proxyFactory.getProxy();
		proxy.Serve("郝志新");
		proxy.Serve("曹快快");
	}
	//@Test
	public void testSpringThrowAdvice(){
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(waiter);
		proxyFactory.addAdvice(springThrowAdvice);
		Waiter proxy = (Waiter)proxyFactory.getProxy();
		proxy.driving("郝志新");
	}
	//*/
	//这个是引介增强
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:SpringAdvice.xml");
		Waiter waitProxy = (Waiter)applicationContext.getBean("waitProxy");
		waitProxy.Serve("haozhixin");
		ManagerMent manager = (ManagerMent)waitProxy;
		manager.manage("haozhixin");

	}

}

  • 引介增强的XML注释


    
    
    
    
            
            
            
            
        
        
            
            

    

以上就是所有增强的代码,我会在下篇博客中将代码上传,欢迎下载!


作者:select you from me
来源:CSDN
转载请联系作者获得授权并注明出处。

你可能感兴趣的:(SpringAOP,Spring增强,注解)