Spring Mvc那点事---(26)Spring Mvc基于Schema的AOP实现

   AOP是面向切面编程,与OOP面向对象编程不同,如果OOP是纵向的,那么AOP就是横向的。使用AOP可以实现很多功能,例如日志记录,安全控制,性能统计,事务控制,异常处理等.AOP中有以下几个重要点。

aspect拦截切面,其实就是一个AOP类,用于执行AOP操作

Joinpoint(连接点):连接点是指那些被拦截到的点。在spring中,就是是方法,
Pointcut(切入点):切入点是指我们要对哪些方法,也就是Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.,pring采用动态代理织入,而AspectJ采用编译期织入和类装在期织入.
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类Aspect(切面): 是切入点和通知(引介)的结合

使用AOP前,我们需要先引入相应的JAR包

 <!-- spring 组件配置 -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

接下来我们创建一个业务类,看看怎么实现AOP操作

1.AOP示例

package com.aopaspect;

public class Person {

	public void Eat() throws Exception
	{
		System.out.println("吃饭");
	}
}
然后我们创建一个切面aspect对业务进行操作。

package com.aopaspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class personAspect {

	//执行前
	public void dobefore()
	{
		System.out.println("吃饭前先洗手");
		
	}
	
	//执行后
	public void doAfterReturning()
	{
		System.out.println("吃完饭后洗碗");
	}
	
	//最终结果
	public void After()
	{
		
	
		System.out.println("饭后吃水果");
	}
	
	//异常通知
	public void AfterThorwing()
	{
		System.out.println("出现异常");
		
	}
	
	//环绕通知
	public Object AroundAdvice(ProceedingJoinPoint p) throws Throwable
	{
		System.out.println("吃饭中看电视");
		Object o=p.proceed();
		System.out.println("吃饭中聊天");
		return o;
	}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
    default-lazy-init="true">

    <description>Spring配置</description>

<!-- 业务类 -->
<bean id="person" class="com.aopaspect.Person"> </bean>

<!-- 切面 -->
<bean id="personAspect" class="com.aopaspect.personAspect"> </bean>

<aop:config>
      <aop:aspect id="aopperson" ref="personAspect"> 
         <aop:pointcut expression="execution(* com.aopaspect.Person.*(..))" id="personPoint"/>
         <!-- 前置通知 -->
         <aop:before method="dobefore"  pointcut-ref="personPoint"  />
         <aop:after-throwing method="AfterThorwing" pointcut-ref="personPoint"/>
         
         <aop:after method="After" pointcut-ref="personPoint"/>
         <!-- 如果出现异常  after-returning将不会被执行-->
         <aop:after-returning method="doAfterReturning" pointcut-ref="personPoint"/> 
         <aop:around method="AroundAdvice" pointcut-ref="personPoint"/>
         
         <aop:declare-parents types-matching="com.aopaspect.Person" implement-interface="com.aopaspect.IStudy" default-impl="com.aopaspect.Study"/>
      </aop:aspect>
</aop:config>
  
</beans>
  public static void main( String[] args )
    {
  
    	 ApplicationContext context = new ClassPathXmlApplicationContext("file:D:/demo/javademo/springAopDemo/src/main/resources/applicationContext.xml");
    	 Person p = (Person) context.getBean("person");
    	 try {
			p.Eat();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
这样一个AOP示例就完成了。我们看下AOP中的方法

使用AOP,首先需要在配置文件中把AOP切面定义一个bean,然后使用<aop:config>进行配置,

使用pointcut指定要对哪些方法进行拦截,使用expression属性可以进行指定


语法:
  execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
例如
  匹配所有类public方法 execution(public * *(..))
  匹配指定包下所有类方法 execution(* cn.itcast.dao.*(..)) 不包含子包
  execution(* cn.itcast.dao..*(..)) ..*表示包、子孙包下所有类
  匹配指定类所有方法 execution(* cn.itcast.service.UserService.*(..))
  匹配实现特定接口所有类方法 execution(* cn.itcast.dao.GenericDAO+.*(..))
  匹配所有save开头的方法 execution(* save*(..))


advice通知主要包括以下几类

before在执行业务方法前执行

aop:after在执行业务方法后执行

 aop:after-returning也是在执行业务方法后执行,如果业务方法执行过程中出现异常,就不会执行该方法

aop:after-throwing 执行业务方法中如果出现异常,则执行该方法

aop:around 环绕通知,在执行方法的同时围绕方法执行。

以为就是aop的简单用法

2.Introduction

Introduction可以为业务方法知道一个接口,并制定接口的实现方法。

package com.aopaspect;

public interface IStudy {

	public void StudyLanage();
}


package com.aopaspect;

public class Study implements IStudy {

	public void StudyLanage() {
		System.out.println("学习汉语");
		
	}

}
添加配置
  <aop:declare-parents types-matching="com.aopaspect.Person" implement-interface="com.aopaspect.IStudy" default-impl="com.aopaspect.Study"/>
types-matching指定匹配哪些业务类

implement-interface指定接口

default-impl指定实现

   public static void main( String[] args )
    {
  
    	 ApplicationContext context = new ClassPathXmlApplicationContext("file:D:/demo/javademo/springAopDemo/src/main/resources/applicationContext.xml");
    	 
    	 IStudy s=(IStudy)context.getBean("person");
    	 s.StudyLanage();
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值