顾问Advisor
  通知 advice

PointcutAdvisor  一个接口  是顾问的一种、

. 任意单个字符

+ 重复1到多次

* 重复0到多次

NameMetchMethodPointcutAdvisor 名称匹配方法顾问

public interface SomeAdiver {
public void daSome();
public void dadent(); }

public class SellAdiver implements SomeAdiver {
public void daSome() {
System.out.println("==========dasome=========");
} public void dadent() {
System.out.println("============dadent=========");
}
}

public class beforeAdvisor implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("===========前置=============");
}
}
applicationContestSpringNameAdiver.xml
<!--目标对象-->
<bean id="SellAdiver" class="cn.happy.NamemethAdvisor.SellAdiver"></bean>
<!--增强-->
<bean id="beforeall" class="cn.happy.NamemethAdvisor.beforeAdvisor"></bean>
<!--增强 顾问-->
<bean id="beforAdivsor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="beforeall"></property>
<property name="mappedNames" value="daSome"></property>
</bean>
<!--aop-->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="SellAdiver"></property>
<property name="interceptorNames" value="beforAdivsor"></property>
</bean>
测试类:
public class SpringAdvisor {
@Test
public void adcisor(){
ApplicationContext cto=new ClassPathXmlApplicationContext("applicationContestSpringNameAdiver.xml");
SomeAdiver sep= (SomeAdiver) cto.getBean("proxyService");
sep.dadent();
sep.daSome();
} }
  测试结果类:
============dadent=========
===========前置=============
==========dasome=========
--------------------------------------------------------------------------------------
  RegexpMethodPointcutAdvisor正则表达式匹配方法切入点顾问
public interface SomeAdiver {
public void daSome();
public void dadent(); }

public class SellAdiver implements SomeAdiver {
public void daSome() {
System.out.println("==========dasome=========");
} public void dadent() {
System.out.println("============dadent=========");
}
}
public class beforeAdvisor implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("===========前置=============");
}
}
applicationContestSpringRegexAdisor.xml
<!--01.目标对象-->
<bean id="someService" class="cn.happy.RegexpMethAdxisor.SellAdiver"></bean> <!--02.增强 通知-->
<bean id="beforeAdvice" class="cn.happy.RegexpMethAdxisor.beforeAdvisor"></bean> <!--增强 正则 顾问-->
<bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="beforeAdvice"></property>
<property name="pattern" value=".*d.*" ></property>
</bean>
<!--03.aop -->
<bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置需要增强的目标对象-->
<property name="target" ref="someService"></property> <!--做怎么样的增强-->
<property name="interceptorNames" value="beforeAdvisor"></property> </bean>

测试类:
public class RegexAdvisor {
@Test
public void Rex(){
ApplicationContext ctk=new ClassPathXmlApplicationContext("applicationContestSpringRegexAdisor.xml");
SomeAdiver som= (SomeAdiver) ctk.getBean("proxyService");
som.daSome();
som.dadent();
}
}
正则表达式测试结果:
===========前置=============
==========dasome=========
===========前置=============
============dadent=========
--------------------------------------------------------------------
 DefaultAdvisorAutoProxyCreator 默认顾问自动代理
public interface SomeProjoin {
public void add();
public void edd();
}
//前置通知
public class MyBefore implements MethodBeforeAdvice{
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("----前置通知----");
}
}
//后置通知
public class MyAfter implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("---后置通知----");
}
}
public class Isomeproing implements SomeProjoin {
public void add() {
System.out.println("-----------再见------------");
} public void edd() {
System.out.println("----------不曾说再见-------");
}
}
applicationContestSpringDefault.xml
<!--目标对象-->
<bean id="ssomeproing" class="cn.happy.DefaultCreator.Isomeproing"></bean>
<!--增强 通知-->
<bean id="beforea" class="cn.happy.DefaultCreator.MyBefore"></bean>
<bean id="after" class="cn.happy.DefaultCreator.MyAfter"></bean> <!--增强 前置顾问-->
<bean id="beforAdivsor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="beforea"></property>
<property name="pattern" value=".*d.*"></property>
</bean>
<!--增强 后置顾问-->
<bean id="afterAdivsor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="after"></property>
<property name="pattern" value=".*d.*"></property>
</bean>
<!--aop-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
默认自动代理测试类;
public class SpringDefault {
@Test
// 03.默认Advisor自动代理生成器
public void defau(){
ApplicationContext con=new ClassPathXmlApplicationContext("applicationContestSpringDefault.xml");
SomeProjoin som= (SomeProjoin) con.getBean("ssomeproing");
som.add();
som.edd();
}
}
 默认自动代理测试类:
----前置通知----
-----------再见------------
---后置通知----
----前置通知----
----------不曾说再见-------
---后置通知----
--------------------------------------------------------------------------------------------
BeanName自动代理生成器
public interface BSome {
public void one();
public void two();
}
public class Bsomeimpl implements BSome {
public void one() {
System.out.println("人生若是无误!");
} public void two() {
System.out.println("铅笔何须橡皮!");
}
}
//前置通知
public class MyBefore implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("************前置通知**************");
}
}

applicationContestSpringBeanName.xml
<!--目标对象-->
<bean id="Bsomeimpl" class="cn.happy.SpringBeanNameCare.Bsomeimpl"></bean>
<!--增强 通知-->
<bean id="beforea" class="cn.happy.SpringBeanNameCare.MyBefore"></bean> <!--增强 前置顾问-->
<bean id="beforAdivsor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="beforea"></property>
<property name="pattern" value=".*d.*"></property>
</bean> <!--aop-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="Bsomeimpl"></property>
<property name="interceptorNames" value="beforea"></property>
</bean>
BeanName测试类:
public class SpringBeanName {
//BeanName自动代理生成器
@Test
public void beanname(){
ApplicationContext appl=new ClassPathXmlApplicationContext("applicationContestSpringBeanName.xml");
BSome sd= (BSome) appl.getBean("Bsomeimpl");
sd.one();
sd.two();
}
}
BeanName测试结果:
************前置通知**************
人生若是无误!
************前置通知**************
铅笔何须橡皮!
---------------------------------------------------------------------------------------------
注解Aspectj
public interface SomeAdiver {
public void daSome();
public void dadent(); }
public class SellAdiver implements SomeAdiver {
public void daSome() {
System.out.println("==========dasome=========");
} public void dadent() {
System.out.println("============dadent=========");
}
}
//前置增强内容
@Before(value = "execution(* *..springaspectj.*.*(..))")
public void mybefore(){
System.out.println("=========前置增强内容=========");
}

//后置增强内容
@AfterReturning(value = "execution(* *..springaspectj.*.*(..))")
public void myafterring(){
System.out.println("========后置增强内容=========");
}

//环绕增强内容
@Around(value ="execution(* *..springaspectj.*.*(..))" )
public Object rouing(ProceedingJoinPoint proceed)throws Throwable{
System.out.println("环绕前增强内容");
Object result=proceed.proceed();
System.out.println("环绕后增强内容");
if(result!=null){
String str= (String) result;
return str.toUpperCase();
}else {
return null;
}
}

applicationContestSpringAspectj.xml
<!--注解Aspectj-->
<!--目标对象-->
<bean id="SellAdiver" class="cn.happy.springaspectj.SellAdiver"></bean>
<!--增强 通知-->
<bean class="cn.happy.springaspectj.MyAsjectsj"></bean>
<aop:aspectj-autoproxy/>

测试类:
public class SpringAspectj {
    @Test
public void tent(){
ApplicationContext apl=new ClassPathXmlApplicationContext("applicationContestSpringAspect.xml");
SomeAdiver ss= (SomeAdiver) apl.getBean("SellAdiver");
ss.dadent();
ss.daSome();
}
}
前置增强内容结果   
      =========前置增强内容=========
      ============dadent=========
=========前置增强内容=========
==========dasome=========

后置增强内容结果 
============dadent=========
========后置增强内容=========
==========dasome=========
========后置增强内容=========

环绕增强内容结果 
  环绕前增强内容
============dadent=========
环绕后增强内容
环绕前增强内容
==========dasome=========
环绕后增强内容
 
 

顾问Advisor Aspectj注解的更多相关文章

  1. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  2. Spring——AOP(面向切面编程)@AspectJ注解方式

    一.什么是AOP? AOP: (Aspect Oriented Programming)即面向切面编程. 试想这样的场景:项目中需要在业务方法执行完打印日志记录.最笨的办法就是在每个方法核心业务执行完 ...

  3. SSM-Spring-17:Spring中aspectJ注解版

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供 ...

  4. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  5. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  6. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  7. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  8. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  9. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

随机推荐

  1. 使用NSTask调用shell

    - (NSString *)cmd:(NSString *)cmd { // 初始化并设置shell路径 NSTask *task = [[NSTask alloc] init]; [task set ...

  2. DDD领域驱动之干货 (一)

    说道DDD不得不说传统的架构与DDD的架构区别. 传统的架构不外乎就是三层,而在这三层里面又不断的细分,始终没有达到想要的效果,那么为什么当时还是采用三层. 当然在DDD没有提出的时候三层是大多数人的 ...

  3. shell之cut和tr 的命令的使用

    [root@data-1-3 ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin ...

  4. 基于aspectj实现AOP操作的两种方式——xml配置

    1. 要导入的 jar 包: 常用的aspectj表达式: 权限修饰符可以省略,以下表示:返回值类型为任意,com.chy.service包以及其子包下的.任意类的.参数任意的.任意方法 execut ...

  5. CF 809D Hitchhiking in the Baltic States——splay+dp

    题目:http://codeforces.com/contest/809/problem/D 如果值是固定的,新加入一个值,可以让第一个值大于它的那个长度的值等于它. 如今值是一段区间,就对区间内的d ...

  6. linux 中的局部变量、全局变量、shell 变量的总结

    系统局部变量和全局变量  一.变量分类局部变量和环境变量,局部变量只适用于当前shell,而环境变量是全局的,它适用于所有当前shell以及其派生出来的任意子进程,有些变量是用户创建的,其他的则是专用 ...

  7. AngularJS系统学习之$watch(监控)

    在scope的内置的所有函数中,用的最多的可能就是$watch函数了, 当你的数据模型中某一部分发生变化时,$watch函数可以向你发出通知. 你可以监控单个对象的属性,亦可以监控需要经过计算的结果( ...

  8. Owin asp.net 脱离 IIS

    http://www.cnblogs.com/jesse2013/p/owin-webserver.html

  9. CF-798C

    C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  10. linux中的条件变量

    1 大家可能知道互斥量是线程程序中必须的工具了,但是也不能是万能的,就比如某个线程正在等待共享数据某个条件的发生,这个时候会发生什么呢.它就可能重复的尝试对互斥对象锁定和解锁来检查共享数据结构. 2 ...