spring-aop实现

aop介绍
 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。
  面向切面是面向对象中的一种方式而已。在代码执行过程中,动态嵌入其他代码,叫做面向切面编程。常见的使用场景:
i :日志
ii: 事务
iii:数据库操作

aop的实现方式(schema)

前置通知(MethodBeforeAdvice)

@Component
public class MyBeforeAdvice implements MethodBeforeAdvice{

@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
 System.out.println("前置通知执行:"+method.getName());
	
}

}

后置通知(AfterReturningAdvice)

@Component
public class AfterAdvice implements AfterReturningAdvice{

@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
 System.out.println("后置通知 :"+returnValue);
	
}

环绕通知(MethodInterceptor)

@Component
public class AroundInterceptor implements MethodInterceptor{

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
	 System.out.println("环绕通知执行1");
	String msg  = (String)invocation.proceed();
	 System.out.println("环绕通知执行2");
	return msg==null?null:msg.toUpperCase();
}

}
异常处理通知(ThrowsAdvice)

@Component
public class MyThrowsAdvice implements ThrowsAdvice {

public void afterThrowing(Exception ex){
	System.out.println("异常参数了:"+ex.getMessage());
}

}

测试类

public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
IWindow window = ac.getBean(“mytarget”,IWindow.class);
window.say();
System.out.println("----------");
System.out.println(window.doSome());
}
}

applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- 开启扫描 -->

<context:component-scan base-package=“com.sxt.*”></context:component-scan>

<!-- 1.注册目标对象 -->
 <bean class="com.sxt.advice.MyBeforeAdvice" id="methodBeforeAdvice"/> 
 <bean class="com.sxt.advice.AfterAdvice" id="afterAdvice"></bean>
 <bean class="com.sxt.advice.AroundInterceptor" id="around"></bean>
 <bean class="com.sxt.advice.MyThrowsAdvice" id="myThrowsAdvice"></bean> 
 <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="mytarget">
	<property name="target" ref="window" />
	<property name="interfaces" value="com.sxt.pojo.IWindow" />
	<property name="interceptorNames">
		<array>
			<!-- 关联前置通知 -->
			<value>methodBeforeAdvice</value>
			<value>afterAdvice</value>
			<value>aroundInterceptor</value>
			<value>myThrowsAdvice</value>
		</array>
	</property>
</bean>

基于aspectJ方式实现

对于AOP这种编程思想,很多框架都进行了实现。Spring就是其中之一,可以完成面向切面编程。然而,AspectJ也实现了AOP的功能,且其实现方式更为简捷,使用更为方便,而且还支持注解式开发。所以,Spring又将AspectJ的对于AOP的实现也引入到了自己的框架中。在Spring中使用AOP开发时,一般使用AspectJ的实现方式

介绍下xml配置的方式 ,注解的很简单就不发了

实体类

public class Window implements IWindow{

@Override
public void say(){
	System.out.println("div  dir");
}

@Override
public String doSome(){
	System.out.println("你好");
	
	return "hello";
}

}

切面类

public class Advice{

  public void before(){
	  System.out.println("前置通知");
  }

 
  public void afterReturning(){
	  System.out.println("后置通知");
  }
  
 
  public Object around(ProceedingJoinPoint pjp) throws Throwable{
	  System.out.println("环绕通知1");
	  Object msg = pjp.proceed();
	  if (msg != null) {
		msg = msg.toString().toUpperCase();
		  System.out.println("环绕通知2");
		return msg;
	}
	  return null;
  }
  
  
  
  public void throwing(Exception ex){
	  System.out.println("参数异常了:"+ex.getMessage());
  }
  
 
  public void after(){
	  System.out.println("最终通知");
  }

}

测试类

public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
IWindow window = ac.getBean(IWindow.class);
window.say();
System.out.println("----------");
System.out.println(window.doSome());
}
}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>





<!-- 3.配置AOP -->
<aop:config>
<!-- 配置切面对象 -->
	<aop:aspect ref="myAdvice">
		<!-- 配置切面点 -->
		<aop:pointcut expression="execution(* com.sxt.pojo.*.*(..))" id="pointcut"/>
		
		<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>	
		<aop:before method="before" pointcut-ref="pointcut"/>	
		<aop:around method="around" pointcut-ref="pointcut"/>	
		<aop:after-throwing method="throwing" pointcut-ref="pointcut" throwing="ex"/>	
		<aop:after method="after" pointcut-ref="pointcut"/>	
	</aop:aspect>
</aop:config>

控制台输入
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值