手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring AOP四种实现方式

[日期:2016-10-13] 来源:Linux社区  作者:Linux [字体: ]

Spring AOP四种实现方式

1. 经典的基于代理的AOP

  1. 创建通知:定义一个接口

public interface Sleepable{
 
    void sleep();
}
然后写一个Human类,他实现了这个接口


public Human implements Sleepable{
 
  public void sleep(){
      System.out.println("睡觉中...!");
  }

}

2.编写一个SleepHelper类,它里面包含了睡觉的辅助工作,用AOP术语来说它就应该是通知

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice  {

    public void before(Method method, Object[] arguments, Object target)
            throws Throwable {
        System.out.println("睡觉前");
       
    }

    public void afterReturning(Object rturnValue, Method method, Object[] arguments,
            Object target) throws Throwable {
        System.out.println("睡觉后");
       
    }

}

然后在spring配置文件中进行配置:

<!-- 被代理目标对象 -->

<bean id="Human" class="cn.happy.dao.Human"></bean>

<bean id="SleepHelper" class="cn.happy.aop.SleepHelper"></bean>
//定义切点的常用的两种方式:1)使用正则表达式 2)使用AspectJ表达式,
//这里用正则表达式

<!-- 顾问 -->
    <bean id="BeforAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
     
        <property name="advice" ref="BeforAdvice"></property>
      <property name="pattern" value=".*l.*g.*"></property>
      </bean>

<!-- 代理对象 -->
// ProxyFactoryBean是一个代理,我们可以把它转换为
//proxyInterfaces中指定的实现该interface的代理对象
      <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
     
        <property name="target" ref="Human"></property>
      <property name="interceptorNames" value="BeforAdvisor"></property>
      </bean>

代理类

public class StuTest {
    public static void main(String[] args) {
        //通过ClassPathXmlApplicationContext实例化Spring的上下文
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过ApplicationContext的getBean()方法,根据id来获取Bean的实例
        Sleepable s=(Sleepable)context.getBean("Human");
        s.sleep();
    }
}

3、使用AspectJ的注解

用@Aspect的注解来标识切面

@Aspect//该类为切面
public class MyAspect {
    @Before(value="execution(public * *(..))")
 public void mybefore(){
   
        System.out.println("前置增强");
 }
    //后置增强
    @AfterReturning(value="execution(public * *(..))")
    public void myafterReturning(){
       
            System.out.println("后置增强");
    }
   
    //异常增强
        @AfterThrowing(value="execution(public * *(..))")
        public void myafterThrowing(){
           
                System.out.println("异常增强");
        }
       
        //环绕增强
        @Around(value="execution(public * *(..))")
        public void myAround(ProceedingJoinPoint jp){
            System.out.println("环绕前增强");
                try {
                    jp.proceed();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("环绕后增强");
               
        }
       
        //最终增强
                @After(value="execution(public * *(..))")
                public void myafterLogger(){
                   
                        System.out.println("最终增强");
                }
}

Spring配置文件:

<!-- 目标对象 -->
  <bean id="Human" class="cn.happy.dao.IserviceImpl"></bean>

  <!-- 切面: -->
  <bean id="myAspect" class="cn.happy.aop.MyAspect"></bean>
 
  <aop:aspectj-autoproxy/>

测试类

public class StuTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Sleepable s=(Sleepable)context.getBean("Service");
        s.sleep();
    }
}

复制代码

4、通过<aop:config>来配置(纯POJO切面)

<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>顶级配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点

public class MyAspect {
 public void mybefore(){
   
        System.out.println("前置增强");
 }
 public String myafterReturning(String Returning){
   
        System.out.println("前置增强");
        return Returning;
}
}


public class IserviceImpl implements Iservice{

    public void log() {
        System.out.println("开启事务");
       
    }

    public String dofirst() {
        System.out.println("记录日志");
        return "";
       
    }

   
}

Spring的配置文件:

<!-- 目标对象 -->
  <bean id="Service" class="cn.happy.dao.IserviceImpl"></bean>

  <!-- 切面: -->
  <bean id="myAspect" class="cn.happy.aop.MyAspect"></bean>
  <!-- 配置切面 -->
  <aop:config>
  <!-- 配置切入点 -->
      <aop:pointcut id="pointcut" expression="execution(public * *..Iservice.log(..))"/>
      <!-- 将类方法定义为最终增强并引用pointcut切入点-->
      <aop:aspect ref="myAspect">
          <aop:after method="myafterReturning" pointcut-ref="pointcut"/>
      </aop:aspect>
  </aop:config>

测试类

public class SpringTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        Iservice bean = (Iservice)context.getBean("Service");
        bean.log();
        String count=bean.dofirst();
        System.out.println(count);
    }
}

Spring AOP自定义注解方式实现日志管理 http://www.linuxidc.com/Linux/2015-11/125019.htm

Spring AOP进行日志记录  http://www.linuxidc.com/Linux/2015-11/124731.htm

使用Spring AOP进行性能监控  http://www.linuxidc.com/Linux/2012-07/64681.htm

利用Spring AOP 更新Memcached 缓存策略的实现  http://www.linuxidc.com/Linux/2012-03/56503.htm

Spring AOP的两种代理  http://www.linuxidc.com/Linux/2015-11/125017.htm

Spring AOP的注解实例 http://www.linuxidc.com/Linux/2015-11/125018.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-10/135993.htm

linux
相关资讯       Spring AOP 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款