顾问(Advisor),通知 advice ,注解Aspectj

  顾问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=========
  环绕后增强内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值