当前位置: 首页 > news >正文

api网站模板可信赖的菏泽网站建设

api网站模板,可信赖的菏泽网站建设,网站建设和设计,怎么注册深圳公司本文讲述使用AspectJ框架实现Spring AOP。 再重复一下Spring AOP中的三个概念#xff0c; Advice#xff1a;向程序内部注入的代码。Pointcut#xff1a;注入Advice的位置#xff0c;切入点#xff0c;一般为某方法。Advisor#xff1a;Advice和Pointcut的结合单元#…本文讲述使用AspectJ框架实现Spring AOP。 再重复一下Spring AOP中的三个概念 Advice向程序内部注入的代码。Pointcut注入Advice的位置切入点一般为某方法。AdvisorAdvice和Pointcut的结合单元以便将Advice和Pointcut分开实现灵活配置。AspectJ是基于注释Annotation的所以需要JDK5.0以上的支持。 AspectJ支持的注释类型如下 BeforeAfterAfterReturningAfterThrowingAround  首先定义一个简单的beanCustomerBo实现了接口ICustomerBo ICustomerBo.java如下 package com.lei.demo.aop.aspectj;public interface ICustomerBo {void addCustomer();void deleteCustomer();String AddCustomerReturnValue();void addCustomerThrowException() throws Exception;void addCustomerAround(String name);}    CustomerBo.java如下   package com.lei.demo.aop.aspectj;public class CustomerBo implements ICustomerBo {public void addCustomer() {System.out.println(addCustomer() is running ...);}public void deleteCustomer() {System.out.println(deleteCustomer() is running ...);}public String AddCustomerReturnValue() {System.out.println(AddCustomerReturnValue() is running ...);return abc;}public void addCustomerThrowException() throws Exception {System.out.println(addCustomerThrowException() is running ...);throw new Exception(Generic Error);}public void addCustomerAround(String name) {System.out.println(addCustomerAround() is running ,args:name);}}   一、      简单的AspectJAdvice和Pointcut结合在一起 首先没有引入Pointcut之前Advice和Pointcut是混在一起的 步骤只需要两步如下 创建一个Aspect类配置Spring配置文件  第一步创建Aspect类 LoggingAspect.java如下 package com.lei.demo.aop.aspectj;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;Aspect public class LoggingAspect {Before(execution(public * com.lei.demo.aop.aspectj.CustomerBo.addCustomer(..)))public void logBefore(JoinPoint joinPoint){System.out.println(logBefore() is running ...);System.out.println(hijacked:joinPoint.getSignature().getName());System.out.println(**********);}After(execution(public * com.lei.demo.aop.aspectj.CustomerBo.deleteCustomer(..)))public void logAfter(JoinPoint joinPoint){System.out.println(logAfter() is running ...);System.out.println(hijacked:joinPoint.getSignature().getName());System.out.println(**********);} }   解释 1.  必须使用Aspect在LoggingAspect声明之前注释以便被框架扫描到 2.  此例Advice和Pointcut结合在一起类中的具体方法logBefore和logAfter即为Advice是要注入的代码Advice方法上的表达式为Pointcut表达式即定义了切入点上例中Before注释的表达式代表执行CustomerBo.addCustomer方法时注入logBefore代码。 3.  在LoggingAspect方法上加入Before或者After等注释 4.  execution(public * com.lei.demo.aop.aspectj.CustomerBo.addCustomer(..))是Aspect的切入点表达式其中*代表返回类型后边的就要定义要拦截的方法名这里写的的是com.lei.demo.aop.aspectj.CustomerBo.addCustomer表示拦截CustomerBo中的addCustomer方法(..)代表参数匹配此处表示匹配任意数量的参数可以是0个也可以是多个如果你确定这个方法不需要使用参数可以直接用()还可以使用(*)来匹配一个任意类型的参数还可以使用 (* , String)这样代表匹配两个参数第二个参数必须是String 类型的参数 5.  AspectJ表达式可以对整个包定义例如execution(* com.lei.service..*.*(..))表示切入点是com.lei.sevice包中的任意一个类的任意方法具体的表达式请自行百度。   第二步配置Spring配置文件 配置Spring-AOP-AspectJ.xml文件如下 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdaop:aspectj-autoproxy/bean idcustomerBo classcom.lei.demo.aop.aspectj.CustomerBo/bean idlogAspect classcom.lei.demo.aop.aspectj.LoggingAspect //beans   解释 1.      aop:aspectj-autoproxy/启动AspectJ支持这样Spring会自动寻找用Aspect注释过的类其他的配置与spring普通bean配置一样。   测试 执行App.java如下 package com.lei.demo.aop.aspectj;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext appContext new ClassPathXmlApplicationContext(new String[] { Spring-AOP-AspectJ.xml });ICustomerBo customer(ICustomerBo)appContext.getBean(customerBo);customer.addCustomer();System.out.println(-------------------------------------------);customer.deleteCustomer();} }   结果 logBefore() is running ... hijacked:addCustomer ********** addCustomer() is running ... ------------------------------------------- deleteCustomer() is running ... logAfter() is running ... hijacked:deleteCustomer **********   二、      将Advice和Pointcut分开 需要三步 创建Pointcut创建Advice配置Spring的配置文件  第一步PointcutsDefinition.java定义了Pointcut如下 package com.lei.demo.aop.aspectj;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut;Aspect public class PointcutsDefinition {Pointcut(execution(* com.lei.demo.aop.aspectj.CustomerBo.*(..)))public void customerLog() {} }   解释 1. 类声明前加入Aspect注释以便被框架扫描到。 2. Pointcut是切入点声明指定需要注入的代码的位置如上例中指定切入点为CustomerBo类中的所有方法在实际业务中往往是指定切入点到一个逻辑层例如 execution (* com.lei.business.service.*.*(..)表示aop切入点为service包中所有类的所有方法具体的表达式后边会有介绍。 3. 方法customerLog是一个签名在Advice中可以用此签名代替切入点表达式所以不需要在方法体内编写实际代码只起到助记功能例如此处代表操作CustomerBo类时需要的切入点。   第二步创建Advice类 LoggingAspect.java如下 package com.lei.demo.aop.aspectj;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;Aspect public class LoggingAspect {Before(com.lei.demo.aop.aspectj.PointcutsDefinition.customerLog())public void logBefore(JoinPoint joinPoint){System.out.println(logBefore() is running ...);System.out.println(hijacked:joinPoint.getSignature().getName());System.out.println(**********);}After(com.lei.demo.aop.aspectj.PointcutsDefinition.customerLog())public void logAfter(JoinPoint joinPoint){System.out.println(logAfter() is running ...);System.out.println(hijacked:joinPoint.getSignature().getName());System.out.println(**********);} }   注释 1.       Before和After使用PointcutsDefinition中的方法签名代替Pointcut表达式找到相应的切入点即通过签名找到PointcutsDefinition中customerLog签名上的Pointcut表达式表达式指定切入点为CustomerBo类中的所有方法。所以此例中Advice类LoggingAdvice为CustomerBo中的所有方法都加入了Before和After两种类型的两种操作。 2.       对于PointcutsDefinition来说主要职责是定义Pointcut可以在其中第一多个切入点并且可以用便于记忆的方法签名进行定义。 3.       单独定义Pointcut的好处是一是通过使用有意义的方法名而不是难读的Pointcut表达式使代码更加直观二是Pointcut可以实现共享被多个Advice直接调用。若有多个Advice调用某个Pointcut而这个Pointcut的表达式在将来有改变时只需修改一个地方维护更加方便。   第三步配置Spring配置文件配置文件并没有改变 配置Spring-AOP-AspectJ.xml文件如下 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdaop:aspectj-autoproxy/bean idcustomerBo classcom.lei.demo.aop.aspectj.CustomerBo/bean idlogAspect classcom.lei.demo.aop.aspectj.LoggingAspect //beans   App.java不变运行测试代码App.java 输出结果 logBefore() is running ... hijacked:addCustomer ********** addCustomer() is running ... logAfter() is running ... hijacked:addCustomer ********** ------------------------------------------- logBefore() is running ... hijacked:deleteCustomer ********** deleteCustomer() is running ... logAfter() is running ... hijacked:deleteCustomer **********   三、     切入点表达式 Spring3.0.5帮助文档中的切入点表达式如下   Some examples of common pointcut expressions are given below.   the execution of any public method: execution(public * *(..)) the execution of any method with a name beginning with set: execution(* set*(..)) the execution of any method defined by the AccountService interface: execution(* com.xyz.service.AccountService.*(..)) the execution of any method defined in the service package: execution(* com.xyz.service.*.*(..)) the execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..)) any join point (method execution only in Spring AOP) within the service package: within(com.xyz.service.*) any join point (method execution only in Spring AOP) within the service package or a sub-package: within(com.xyz.service..*) any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface: this(com.xyz.service.AccountService) this is more commonly used in a binding form :- see the following section on advice for how to make the proxy object available in the advice body.   any join point (method execution only in Spring AOP) where the target object implements the AccountService interface: target(com.xyz.service.AccountService) target is more commonly used in a binding form :- see the following section on advice for how to make the target object available in the advice body.   any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable: args(java.io.Serializable)args is more commonly used in a binding form :- see the following section on advice for how to make the method arguments available in the advice body. Note that the pointcut given in this example is different to execution(* *(java.io.Serializable)): the args version matches if the argument passed at runtime is Serializable, the execution version matches if the method signature declares a single parameter of type Serializable.   any join point (method execution only in Spring AOP) where the target object has an Transactional annotation: target(org.springframework.transaction.annotation.Transactional) target can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.   any join point (method execution only in Spring AOP) where the declared type of the target object has an Transactional annotation: within(org.springframework.transaction.annotation.Transactional) within can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.   any join point (method execution only in Spring AOP) where the executing method has an Transactional annotation: annotation(org.springframework.transaction.annotation.Transactional) annotation can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.   any join point (method execution only in Spring AOP) which takes a single parameter, and where the runtime type of the argument passed has theClassified annotation: args(com.xyz.security.Classified) args can also be used in a binding form :- see the following section on advice for how to make the annotation object(s) available in the advice body.   any join point (method execution only in Spring AOP) on a Spring bean named tradeService: bean(tradeService) any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression *Service: bean(*Service)转载于:https://www.cnblogs.com/jcomet/p/5570460.html
http://www.zqtcl.cn/news/629892/

相关文章:

  • 一个静态网站多少钱谷歌推广公司哪家好
  • 做体育的网站c2c跨境电商平台有哪些?
  • 山西响应式网站建设推荐全国企业信用信息公示系统浙江
  • 西安做网站维护的公司百度百科官网入口
  • 网站网站建设公司贵阳网站设计阳光创信好吗
  • 网站广告投放收费标准长沙公司制作网站费用
  • 网站建设有哪些环节做一个产品网站要多少钱
  • 公司网站建设价格河北网站制作 网站开发
  • 适合新手做的网站项目职业技术培训
  • 提高网站流量原则昆山做百度网站
  • 怎样设计自己的网站长春制作门户网站的公司
  • 亚马逊商标备案是否必须做网站Wordpress做APP后端
  • 主办单位性质与网站名称不符网站域名怎么买
  • 帝国cms下载类网站怎么做广州外贸营销网站建设公司
  • 网站开发软件开发流程免费做外贸的网站平台有哪些
  • 教育培训网站开发广告公司怎么设置网站关键字
  • 绩溪建设银行网站济南网站建设 刘彬彬
  • 网站开发是打代码吗建网站来做什么
  • 制作网站需要什么软件wordpress建站程序
  • 做网站网站怎么赚钱软件工程师证书报考时间
  • 手机和电脑网站分开做炒股软件下载
  • 网站建设需要注意哪些关键细节杭州做商务网站
  • 做网站,图片显示不出来网站图标代码
  • 理财网网站开发源码h5淘宝网网页版入口
  • 免费网站商城模板宁波企业网站搭建图片
  • 上海网站备案查询建站图标素材
  • 贵州省住房和建设厅网网站网站页面设计报告
  • 做网站友汇网快速建设网站视频教程
  • 物流公司做网站注重什么官网的网站设计公司
  • 网站备案 2016电子商务平台起名