利用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. Java之内部类(1) - 为什么需要内部类

    为什么需要内部类 一般来说,内部类继承自某个类或实现某个接口,内部类的代码操作创建它的外围类的对象.所以可以认为内部类提供了某种进入其外围类的窗口. 内部类必须要回答的一个问题是:如果只是需要一个对接 ...

  2. 【Hibernate步步为营】--锁机制具体解释

    上篇文章具体讨论了hql的各种查询方法.在讨论过程中写了代码演示样例.hql的查询方法类似于sql,查询的方法比較简单,有sql基础的开发者在使用hql时就会变得相当的简单. Hibernate在操作 ...

  3. kettle--组件(2)--计算器

    组件如下: 对计算类型的说明如下: The table below contains descriptions associated with the calculator step: Functio ...

  4. [J2EE基础]初识JSP和Servlet

    近期须要用到J2EE,就開始学习与J2EE相关的知识了. JSP是一种Javaserver端技术,它用于在网页上显示动态内容. Tomcat相关知识 JSP的运行过程 JSP的页面构成元素 JSP的凝 ...

  5. NoSQL(二)

    redis介绍 1.aof存储的文件会越来越大,当文件很大时我们可以进行一次rdb存储原来的aof文件就可以删除了,因为aof就相当与mysql中的binlog文件会一致增长,当redis里面的key ...

  6. python-数据结构Data Structure1

    四种数据结构: 列表list = [val1,val2,val3,val4]字典dict = {key1:val1,key2:val2}元组tuple = (val2,val2,val3,val4)集 ...

  7. 常用 cdn

    http://www.bootcdn.cn/ jquery <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.j ...

  8. 深度CTR预估模型中的特征自动组合机制演化简史 zz

    众所周知,深度学习在计算机视觉.语音识别.自然语言处理等领域最先取得突破并成为主流方法.但是,深度学习为什么是在这些领域而不是其他领域最先成功呢?我想一个原因就是图像.语音.文本数据在空间和时间上具有 ...

  9. atitit. 分销系统规划p8k

    atitit. 分销系统规划p8k 1. 商户平台管理 overview2 1.1. 分销业务管理2 1.2. 文案管理2 1.3. 订单管理3 1.4. 统计报表3 1.5. 财务结算3 1.6.  ...

  10. HTTP Range header

    http://stackoverflow.com/questions/3303029/http-range-header *************************** 58 down vot ...