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

CustomerDao接口

 package com.alphajuns.demo1;

 public interface CustomerDao {

     public void save();

     public void update();

 }

CustomerDaoImpl实现类

 package com.alphajuns.demo1;

 public class CustomerDaoImpl implements CustomerDao {

     @Override
public void save() {
System.out.println("保存客户...");
} @Override
public void update() {
System.out.println("更新客户...");
} }

切面类

 package com.alphajuns.demo1;

 import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; /*
* 注解方式的切面类
*/
@Aspect
public class MyAspectAnno { /*
* 前置通知
*/
@Before(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
public void log() {
System.out.println("记录日志...");
} /*
* 后置通知
*/
/*@After(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")*/
@After(value="MyAspectAnno.fn()")
public void after() {
System.out.println("更新日志...");
} @Around(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("环绕通知1...");
try {
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知2...");
} /*
* 自定义切入点
*/
@Pointcut(value="execution(public * com.alphajuns.demo1.CustomerDaoImpl.save())")
public void fn(){}; }

测试类

 package com.alphajuns.demo1;

 import javax.annotation.Resource;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 { @Resource(name="customerDao")
private CustomerDao customerDao; @Test
public void run1() {
customerDao.save();
} }

配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启自动代理 -->
<aop:aspectj-autoproxy/> <!-- 创建目标对象 -->
<bean id="customerDao" class="com.alphajuns.demo1.CustomerDaoImpl"/> <!-- 创建切面类 -->
<bean id="myAspectAnno" class="com.alphajuns.demo1.MyAspectAnno"/> </beans>

Spring框架中的AOP技术----注解方式的更多相关文章

  1. Spring框架中的AOP技术----配置文件方式

    1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...

  2. spring框架中的aop技术

    1. 什么是AOP, 面向切面编程 AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的 ...

  3. Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束

    (aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...

  4. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  5. Spring框架中的aop操作之二 通过配置文件实现增强

    aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  6. Spring Security框架中踢人下线技术探索

    1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...

  7. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  8. spring框架中的@Import注解

    spring框架中的@Import注解 Spring框架中的@Import注解 在之前的文章中,作者介绍了Spring JavaConfig. 这是除了使用传统的XML文件之外,spring带来的新的 ...

  9. Spring Boot中使用AOP统一处理Web请求日志

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...

随机推荐

  1. H5 Canvas | 画布中变量作用域与setInterval方法同步执行探究

    Demo - 随机绘制圆环 实现思路: 将一个圆环的绘制分成100份,setInterval()方法定义每隔时间n绘制一段新的,每份的开始路径都是上一次的结束路径,实现步进绘制. 通过Math.ran ...

  2. c#导出到excel的几种解决方案

    (一)传统操作Excel遇到的问题: 1.如果是.NET[使用office组件Microsoft.Iffice.interop.Excel的话],需要在服务器端装Office,且及时更新它,以防漏洞, ...

  3. lucene 分词器

    分词器 作用:切分关键词的. 在什么地方使用到了:在建立索引和搜索时. 原文:An IndexWriter creates and maintains an index. 1,切分: An Index ...

  4. Android数据格式解析对象JSON用法

    1.JSON概念: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性,从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. JSON可以将 ...

  5. 【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

    [075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array with n objects colore ...

  6. C++程序设计项目开发——银行自己主动提款机(二)

    函数的有关知识在后面章节会讲到,先提前了解下.在没有系统的学习完之前,咱们先来模仿着写一个样例,尝试这样的有效的学习方法.   尝试下这种学习方法. 显示功能选项 1.查询 2.取款 3.存款 4.转 ...

  7. WebSocket遇到的一些问题

    一 .Nginx配置websocket   为了解决Nginx转发不能进行websocket通信问题 将nginx配置文件添加如下内容:   map $http_upgrade $connection ...

  8. atitit.jquery tmpl模板总结 .doc

    atitit.jquery tmpl模板总结 .doc 1. atitit.动态模版解析1 1.1. Jquery.tmpl.js1 1.2. 比起anrular js方便啊.1 2. 动态模板引擎解 ...

  9. [求助] 关于DDR3的读写操作,看看我的流程对吗?

    [求助] 关于DDR3的读写操作,看看我的流程对吗? 最近简单调了一下KC705开发板上面的DDR3,型号是MT8JTF12864HZ-1G6:有时候加载程序后,发现读出数据不是写进去的,在这将我的操 ...

  10. maven下载源码和java docs

    方法一: mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc The first command will attem ...