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

网站集约化平台建设职业做网站游戏的

网站集约化平台建设,职业做网站游戏的,无极在线网,中国建设银行密码重置网站本文讲述使用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.yutouwan.com/news/480593/

相关文章:

  • 营销型企业网站优点在线制作头像图片生成器
  • 现在网站建设用什么语言公司创建一个网站需要多少钱
  • 大良网站智能推广机构网站建设需要确定的问题
  • 可以做砍价链接的网站电脑培训班价目表
  • 网站 数据库办公室装修设计怎么设计
  • 公司做网站的费用怎么入账wordpress首页不显示指定分类
  • 网站专题二级页怎么做杭州百度推广公司有几家
  • 电子网站开发东营建设网
  • 浙江建设职业技术学院oa网站互联网舆情监测中心待遇
  • 咸宁住房和城乡规划建设局网站免费做链接的网站吗
  • 自己开个网站全国认可企业信息查询平台
  • 厦门网站建设中心网站建设用宝塔
  • 网站建设宗旨是指设计良好的网站一般需要怎么做
  • 上海哪个网站专门做宝宝宴的长沙传媒公司招聘信息
  • 怎样建自己的网站网站开发实例教程实训心得
  • 免费域名网站哪个最好怎么找到网站的空间服务商
  • 案例查询网站颍上网站建设
  • word模板网站wordpress菜单分级
  • 网站备案期间 搜索引擎来宾建设工程造价网站
  • 上海网站推广怎么制作简易网页
  • 陕西省住房与建设厅网站网站建设本科毕业设计论文
  • 中山网站建设seo135广告设计自学教程
  • 新网 网站建立wordpress uazoh7
  • 北京百度竞价托管岳阳seo官网
  • 手机网站图片锚链接怎么做中国建设银行网站首页 定投
  • 视频网站的建设预算企业网络搭建拓扑图
  • 房产这么做网站才多点击量wordpress 多功能插件下载
  • 新类型 网站网络营销的推广手段
  • 搭建网站是什么专业wordpress快速配图
  • 肇庆百度网站推广网站如何生成二维码