zoukankan      html  css  js  c++  java
  • Spring框架中的AOP技术----注解方式

    利用AOP技术注解的方式对功能进行增强

    CustomerDao接口

    1 package com.alphajuns.demo1;
    2 
    3 public interface CustomerDao {
    4 
    5     public void save();
    6     
    7     public void update();
    8     
    9 }

    CustomerDaoImpl实现类

     1 package com.alphajuns.demo1;
     2 
     3 public class CustomerDaoImpl implements CustomerDao {
     4 
     5     @Override
     6     public void save() {
     7         System.out.println("保存客户...");
     8     }
     9 
    10     @Override
    11     public void update() {
    12         System.out.println("更新客户...");
    13     }
    14 
    15 }

    切面类

     1 package com.alphajuns.demo1;
     2 
     3 import org.aspectj.lang.ProceedingJoinPoint;
     4 import org.aspectj.lang.annotation.After;
     5 import org.aspectj.lang.annotation.Around;
     6 import org.aspectj.lang.annotation.Aspect;
     7 import org.aspectj.lang.annotation.Before;
     8 import org.aspectj.lang.annotation.Pointcut;
     9 
    10 /*
    11  * 注解方式的切面类
    12  */
    13 @Aspect
    14 public class MyAspectAnno {
    15 
    16     /*
    17      * 前置通知
    18      */
    19     @Before(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
    20     public void log() {
    21         System.out.println("记录日志...");
    22     }
    23     
    24     /*
    25      * 后置通知
    26      */
    27     /*@After(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")*/
    28     @After(value="MyAspectAnno.fn()")
    29     public void after() {
    30         System.out.println("更新日志...");
    31     }
    32     
    33     @Around(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
    34     public void around(ProceedingJoinPoint joinPoint) {
    35         System.out.println("环绕通知1...");
    36         try {
    37             joinPoint.proceed();
    38         } catch (Throwable e) {
    39             e.printStackTrace();
    40         }
    41         System.out.println("环绕通知2...");
    42     }
    43     
    44     /*
    45      * 自定义切入点
    46      */
    47     @Pointcut(value="execution(public * com.alphajuns.demo1.CustomerDaoImpl.save())")
    48     public void fn(){};
    49     
    50 }

    测试类

     1 package com.alphajuns.demo1;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.test.context.ContextConfiguration;
     8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     9 
    10 @RunWith(SpringJUnit4ClassRunner.class)
    11 @ContextConfiguration("classpath:applicationContext.xml")
    12 public class Demo1 {
    13 
    14     @Resource(name="customerDao")
    15     private CustomerDao customerDao;
    16     
    17     @Test
    18     public void run1() {
    19         customerDao.save();
    20     }
    21     
    22 }

    配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     8     http://www.springframework.org/schema/beans/spring-beans.xsd
     9     http://www.springframework.org/schema/context
    10     http://www.springframework.org/schema/context/spring-context.xsd
    11     http://www.springframework.org/schema/aop
    12     http://www.springframework.org/schema/aop/spring-aop.xsd
    13     http://www.springframework.org/schema/tx 
    14     http://www.springframework.org/schema/tx/spring-tx.xsd">
    15     
    16     <!-- 开启自动代理 -->
    17     <aop:aspectj-autoproxy/>
    18     
    19     <!-- 创建目标对象 -->
    20     <bean id="customerDao" class="com.alphajuns.demo1.CustomerDaoImpl"/>
    21     
    22     <!-- 创建切面类 -->
    23     <bean id="myAspectAnno" class="com.alphajuns.demo1.MyAspectAnno"/>
    24     
    25 </beans>
  • 相关阅读:
    java web 里的JSP 对象的简单了解
    PLSQL 语言 异常 函数 存储过程
    PLSQL语言 分支循环
    高级查询
    oracle 杂项
    sql 里面的常用函数
    java所有内容练习
    线程
    oracle基础 知识点

  • 原文地址:https://www.cnblogs.com/alphajuns/p/10046663.html
Copyright © 2011-2022 走看看