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

做腰椎核磁证网站是 收 七/廊坊今日头条新闻

做腰椎核磁证网站是 收 七,廊坊今日头条新闻,深圳网站建设简介,义乌高端网站建设上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你…

上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都被自动的拦截了。但是大多情况下,你只需要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它允许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。

 

在Spring AOP中,有3个常用的概念,Advices、Pointcut、Advisor,解释如下,

Advices:表示一个method执行前或执行后的动作。

Pointcut:表示根据method的名字或者正则表达式去拦截一个method。

Advisor:Advice和Pointcut组成的独立的单元,并且能够传给proxy factory 对象。

 

下边来回顾一下上一篇例子中的代码

CustomerService.java

package com.lei.demo.aop.advice;public class CustomerService {private String name;private String url;public void setName(String name) {this.name = name;}public void setUrl(String url) {this.url = url;}public void printName() {System.out.println("Customer name : " + this.name);}public void printURL() {System.out.println("Customer website : " + this.url);}public void printThrowException() {throw new IllegalArgumentException();}}

 

 配置文件Spring-AOP-Advice.xml:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"><property name="name" value="LeiOOLei" /><property name="url" value="http://www.cnblogs.com/leiOOlei/" /></bean><bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /><bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="customerService" /><property name="interceptorNames"><list><value>hijackAroundMethodBean</value></list></property></bean></beans>

 

HijackAroundMethod.java

package com.lei.demo.aop.advice;import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;public class HijackAroundMethod implements MethodInterceptor {public Object invoke(MethodInvocation methodInvocation) throws Throwable {System.out.println("Method name : "+ methodInvocation.getMethod().getName());System.out.println("Method arguments : "+ Arrays.toString(methodInvocation.getArguments()));// 相当于  MethodBeforeAdviceSystem.out.println("HijackAroundMethod : Before method hijacked!");try {// 调用原方法,即调用CustomerService中的方法Object result = methodInvocation.proceed();// 相当于 AfterReturningAdviceSystem.out.println("HijackAroundMethod : After method hijacked!");return result;} catch (IllegalArgumentException e) {// 相当于 ThrowsAdviceSystem.out.println("HijackAroundMethod : Throw exception hijacked!");throw e;}}}

 

运行如下App.java

package com.lei.demo.aop.advice;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-Advice.xml" });System.out.println("使用Spring AOP 如下");CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");System.out.println("*************************");cust.printName();System.out.println("*************************");cust.printURL();System.out.println("*************************");try {cust.printThrowException();} catch (Exception e) {}}}

 

 运行结果:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Method name : printURL

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer website : http://www.cnblogs.com/leiOOlei/

HijackAroundMethod : After method hijacked!

*************************

Method name : printThrowException

Method arguments : []

HijackAroundMethod : Before method hijacked!

HijackAroundMethod : Throw exception hijacked!

 

上边的结果中,CustomerService.java中,全部的method方法全部被拦截了,下边我们将展示怎样利用Pointcuts只拦截printName()。

 

你可以用名字匹配法和正则表达式匹配法去匹配要拦截的method。

1.      Pointcut——Name match example

通过pointcut和advisor拦截printName()方法。

创建一个NameMatchMethodPointcut的bean,将你想拦截的方法的名字printName注入到属性mappedName,如下

<bean id="customerPointcut"class="org.springframework.aop.support.NameMatchMethodPointcut"><property name="mappedName" value="printName" />
</bean>

 

 创建一个DefaultPointcutAdvisor的advisor bean,将pointcut和advice关联起来。

<bean id="customerAdvisor"class="org.springframework.aop.support.DefaultPointcutAdvisor"><property name="pointcut" ref="customerPointcut" /><property name="advice" ref=" hijackAroundMethodBean " />
</bean>

 

更改代理的interceptorNames值,将上边的advisor( customerAdvisor)替代原来的hijackAroundMethodBean。

<bean id="customerServiceProxy"class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="customerService" /><property name="interceptorNames"><list><value>customerAdvisor</value></list></property>
</bean>

 

所有的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"><property name="name" value="LeiOOLei" /><property name="url" value="http://www.cnblogs.com/leiOOlei/" /></bean><bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /><bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="customerService" /><property name="interceptorNames"><list><value>customerAdvisor</value></list></property></bean><bean id="customerPointcut"class="org.springframework.aop.support.NameMatchMethodPointcut"><property name="mappedName" value="printName" /></bean><bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><property name="pointcut" ref="customerPointcut" /><property name="advice" ref=" hijackAroundMethodBean " /></bean></beans>

 

 再运行一下App.java,输出结果如下:

使用Spring AOP 如下

*************************

Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!

Customer name : LeiOOLei

HijackAroundMethod : After method hijacked!

*************************

Customer website : http://www.cnblogs.com/leiOOlei/

*************************

 

以上运行结果显示,只拦截了printName()方法。

 

注意:

以上配置中pointcut和advisor可以合并在一起配置,即不用单独配置customerPointcutcustomerAdvisor,只要配置customerAdvisor时class选择NameMatchMethodPointcutAdvisor如下:

<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"><property name="mappedName" value="printName" /><property name="advice" ref="hijackAroundMethodBean" />
</bean>

 

这样,整个配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"><property name="name" value="LeiOOLei" /><property name="url" value="http://www.cnblogs.com/leiOOlei/" /></bean><bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /><bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="customerService" /><property name="interceptorNames"><list><value>customerAdvisor</value></list></property></bean><bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"><property name="mappedName" value="printName" /><property name="advice" ref="hijackAroundMethodBean" /></bean></beans>

 

  实际上这种做法将method名字与具体的advice捆绑在一起,有悖于Spring松耦合理念,如果将method名字单独配置成pointcut(切入点),advice和pointcut的结合会更灵活,使一个pointcut可以和多个advice结合。

2.      Pointcut——Regular exxpression match example

你可以配置用正则表达式匹配需要拦截的method,如下配置

<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="patterns"><list><value>.*URL.*</value></list></property><property name="advice" ref="hijackAroundMethodBeanAdvice" /></bean>

 

现在,你可以拦截名字中包含URL字符的method了,在实际工作中,你可以用它来管理DAO层,例如,你可以用“.*DAO.*”来拦截所有DAO层中的相关业务。

转载于:https://www.cnblogs.com/jcomet/p/5570454.html

http://www.jmfq.cn/news/4892383.html

相关文章:

  • 重庆网站建设流程/上海有什么seo公司
  • 互联网app推广具体怎么做/搜索引擎营销与seo优化
  • 提供网站建设公司哪家好/新媒体seo指的是什么
  • 网站建设推广哪个好/网络广告的收费模式有哪些
  • 做介绍自己的短视频网站/网站页面分析
  • 望江县住房和城乡建设局网站/武汉百度快照优化排名
  • 网站建设是属于虚拟产品吗/如何能查到百度搜索排名
  • 企业购 网站建设/友情链接格式
  • 国外展柜网站/金花关键词工具
  • 如何自己创办一个网站/bt磁力狗
  • 做首饰网站/济南seo外包公司
  • 烟台市芝罘区建设局网站/单页网站模板
  • 做设计在哪个网站找图片/seo资讯推推蛙
  • 自己能不能做个网站/免费crm客户管理系统
  • 红色简约的手机社区类网站html5响应式模板下载/google搜索引擎官网
  • 有做销售产品的网站有哪些内容/如何做到精准客户推广
  • 手机制作图片的软件免费/济源新站seo关键词排名推广
  • 给网站做压力测试/百度seo怎么提高排名
  • 自己搭建云服务平台/英语seo
  • 电子商城网站建设方案/电商代运营公司排名
  • 重庆公司网站建设价格/百度推广视频
  • 三五互联网站管理登录地址是多少/steam交易链接怎么改
  • 赤峰网站策划/深圳sem竞价托管
  • 怎么做代购彩票网站/济南网站优化公司排名
  • 湟源县wap网站建设公司/西安百度seo
  • 网站建设实训报告心得/网站外链是什么
  • 网站设计师/关键词推广
  • 贵州 网站建设/百度竞价广告怎么投放
  • 做seo需要哪些知识/安全优化大师下载
  • 铜山区建设局网站/农大南路网络营销推广优化