全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Spring AOP的几种实现方式总结

AOP核心概念

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

Spring 实现AOP所需要的包:

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

Spring 实现AOP的方式:

1、Java动态代理

该方法针对接口的实例创建代理

applicationContext.xml的配置如下:

<?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:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 
     
    <bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" /> 
  
    <bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" /> 
     
    <aop:config> 
      <aop:aspect id="interceptor" ref="interceptorHandler"> 
        <aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" /> 
        <aop:before method="doSomething" pointcut-ref="addAllMethod" /> 
        <aop:after method="doSomething" pointcut-ref="addAllMethod" /> 
      </aop:aspect> 
    </aop:config> 
</beans> 

其中Abstration为接口,ConcreteImplementor为实现类,InterceptorHandler为代理拦截类。

public interface <span style="font-size:12px;">Abstration</span> { 
  public void operation() 
} 
//具体实现化角色 
public class ConcreteImplementor implements Implementor{ 
 
  @Override 
  public void operation() {   
    System.out.println("ConcreteImplementor"); 
  } 
 
} 
public class InterceptorHandler{  
  public void printTime(){ 
    System.out.println("CurrentTime = " + System.currentTimeMillis()); 
  } 
} 

2、CGLIB生成代理

CGLIB针对代理对象为类的情况使用。

通过实现MethodInterceptor接口,并实现 public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。

3、BeanNameAutoProxyCreator实现AOP

Spring为我们提供了自动代理机制,让容器为我们自动生成代理,把我们从烦琐的配置工作中解放出来,在内部,Spring 使用BeanPostProcessor自动地完成这项工作。

具体配置如下: 

<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>  
<bean  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  <property name="beanNames">  
    <list>  
      <value>*Service</value>  
    </list>  
  </property>  
  <property name="interceptorNames">  
    <list>  
      <value>MyInterceptor</value>  
    </list>  
  </property>  
 </bean> 

其中*Service 为需要拦截代理的bean,以Service结尾的都 被拦截,并使用MyInterceptor 进行拦截,可配置多个拦截器,按顺序执行。

import java.lang.reflect.Method; 
import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
/** 
 * @author 
 * 
 */  
public class MyInterceptor implements MethodInterceptor{  
  
  @Override  
  public Object invoke(MethodInvocation invocation) throws Throwable {  
      
    Method method = invocation.getMethod();//获取被拦截的方法  
    Object[] arguments = invocation.getArguments();//获取拦截方法的参数  
    /* 
     * 特殊,某些权限需要做特殊处理 
     * 比如用户信息权限,在方法执行完毕返回的时候,要将电话号码与邮箱抹除 
     */  
    //环绕通知前置特殊处理  
    this.beforeReslove();  
    Object proceed = invocation.proceed();//调用目标方法  
    //环绕通知后置特殊处理  
    proceed = this.afterReslove();  
    return proceed;  
  } 
   private Object afterReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
     return null; 
   } 
   private void beforeReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
   }   
} 

4、使用注解AspectJ实现AOP

ApplicationContext.xml 加入

<aop:aspectj-autoproxy/> 

 创建切面处理类

package com.marving.aop; 
import java.util.Arrays; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component;  
@Aspect 
@Component  
public class AspectHandler { 
   
  @Pointcut("execution(* com.marving.service.BaseServ+.*(..))") 
  private void doMethod() { 
  } 
 
    /** 
   * This is the method which I would like to execute before a selected method 
   * execution. 
   */ 
  @Before("doMethod()") 
  public void beforeAdvice() { 
    System.out.println("before method invoked."); 
  } 
 
  /** 
   * This is the method which I would like to execute after a selected method 
   * execution. 
   */ 
  @After("doMethod()") 
  public void afterAdvice() { 
    System.out.println("after method invoked."); 
  } 
 
  // 配置controller环绕通知,使用在方法aspect()上注册的切入点 
  @Around("doMethod()") 
  public Object around(ProceedingJoinPoint pjp) throws Throwable{ 
    Object result = null; 
    String methodName = pjp.getSignature().getName(); 
    try { 
      System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs())); 
      result = pjp.proceed(); 
    } catch (Throwable e) { 
      System.out.println("The method [" + methodName + "] occurs expection : " + e); 
      throw new RuntimeException(e); 
    } 
    System.out.println("The method [" + methodName + "] ends"); 
    return result; 
  } 
} 

通过表达式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入点函数,并使用@Before@After@Around 对所拦截方法执行前、中、后进行拦截并执行处理函数。

@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。

值得注意的是,Around在拦截方法后,需要返回一个方法执行结果,否则,原方法不能正常执行。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# spring实现aop的方式  # spring  # aop实现方式  # aop实现  # 深入浅析Spring 的aop实现原理  # 谈谈Spring AOP中@Aspect的高级用法示例  # Spring AOP注解失效的坑及JDK动态代理  # 利用spring AOP记录用户操作日志的方法示例  # 利用Spring AOP记录方法的执行时间  # Spring AOP + 注解实现统一注解功能  # 详解spring面向切面aop拦截器  # spring基础概念AOP与动态代理理解  # 浅谈spring aop的五种通知类型  # 一篇文章从无到有详解Spring中的AOP  # 是在  # 横切  # 的是  # 还可以  # 多个  # 要将  # 所需要  # 不能正常  # 自动生成  # 大家多多  # 就是指  # 怎么处理  # 称之为  # 期为  # 五类  # 在内部  # 前提下  # 拦截器  # config  # interceptorHandler 


相关文章: 网站制作大概多少钱一个,做一个平台网站大概多少钱?  高端建站三要素:定制模板、企业官网与响应式设计优化  如何选择CMS系统实现快速建站与SEO优化?  如何配置IIS站点权限与局域网访问?  如何在西部数码注册域名并快速搭建网站?  制作电商网页,电商供应链怎么做?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  北京营销型网站制作公司,可以用python做一个营销推广网站吗?  php条件判断怎么写_ifelse和switchcase的使用区别【对比】  完全自定义免费建站平台:主题模板在线生成一站式服务  建站VPS能否同时实现高效与安全翻墙?  南宁网站建设制作定制,南宁网站建设可以定制吗?  定制建站模板如何实现SEO优化与智能系统配置?18字教程  *服务器网站为何频现安全漏洞?  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  临沂网站制作企业,临沂第三中学官方网站?  如何选择高效稳定的ISP建站解决方案?  专业商城网站制作公司有哪些,pi商城官网是哪个?  建站之星如何优化SEO以实现高效排名?  如何获取免费开源的自助建站系统源码?  网站制作需要会哪些技术,建立一个网站要花费多少?  如何高效完成独享虚拟主机建站?  高端建站如何打造兼具美学与转化的品牌官网?  微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?  C++中的Pimpl idiom是什么,有什么好处?(隐藏实现)  如何确保FTP站点访问权限与数据传输安全?  小说建站VPS选用指南:性能对比、配置优化与建站方案解析  C#如何序列化对象为XML XmlSerializer用法  开心动漫网站制作软件下载,十分开心动画为何停播?  建站之星多图banner生成与模板自定义指南  如何通过建站之星自助学习解决操作问题?  深圳网站制作平台,深圳市做网站好的公司有哪些?  香港服务器租用每月最低只需15元?  北京网站制作的公司有哪些,北京白云观官方网站?  如何打造高效商业网站?建站目的决定转化率  C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换  如何通过.red域名打造高辨识度品牌网站?  制作旅游网站html,怎样注册旅游网站?  如何用西部建站助手快速创建专业网站?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  微网站制作教程,我微信里的网站怎么才能复制到浏览器里?  网站制作新手教程,新手建设一个网站需要注意些什么?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  教程网站设计制作软件,怎么创建自己的一个网站?  官网建站费用明细查询_企业建站套餐价格及收费标准指南  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  实例解析angularjs的filter过滤器  网站制作公司,橙子建站是合法的吗? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。