主要是切面定义。

与【Spring】AOP(一)使用Spring的API接口不同的是:这种方式使用自定义来实现AOP,而不是调用Spring的API。

下面直接上例子。(与上一个例子的区别在于,增加了diy,修改了applicationContext.xml,删掉了Log)

老规矩,先展示项目结构:

下面是各文件的代码。

DiyPointCut.java

package com.company.org.diy;public class DiyPointCut {public void before(){System.out.println("======== 方法执行前 =======");}public void after(){System.out.println("======== 方法执行后 =======");}
}

applicationContext.xml

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 注册beans --><bean id="userService" class="com.company.org.service.UserServiceImpl"/><bean id="beforelog" class="com.company.org.Log.BeforeLog"/><bean id="afterlog" class="com.company.org.Log.AfterLog"/><!-- 方式二:自定义类 --><bean id="diy" class="com.company.org.diy.DiyPointCut"/><aop:config><!-- 自定义切面,ref要引用的类 --><aop:aspect ref="diy"><!-- 切入点 --><aop:pointcut id="point" expression="execution(* com.company.org.service.UserServiceImpl.*(..))"/><!-- 通知 --><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config></beans>

UserService.java

package com.company.org.service;public interface UserService {public void add();public void delete();public void update();public void select();
}

UserServiceImpl.java

package com.company.org.service;public class UserServiceImpl implements UserService {public void add() {System.out.println("增加了一个用户");}public void delete() {System.out.println("删除了一个用户");}public void update() {System.out.println("更新了一个用户");}public void select() {System.out.println("查询了一个用户");}
}

Mytest.java

import com.company.org.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 动态代理的是接口UserService userService = (UserService) context.getBean("userService");userService.add();}
}

运行结果:

【Spring】AOP(二)自定义来实现AOP相关推荐

  1. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

  2. Spring学习8-Spring事务管理(AOP/声明式式事务管理)

    一.基础知识普及 声明式事务的事务属性: 一:传播行为 二:隔离级别 三:只读提示 四:事务超时间隔 五:异常:指定除去RuntimeException其他回滚异常.  传播行为: 所谓事务的传播行为 ...

  3. Spring学习4-面向切面(AOP)之Spring接口方式

    一.初识AOP    关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节       1.AOP:Aspect ...

  4. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  5. 自定义注解加AOP怎么玩?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 乱敲代码 来源 | 公众号「乱敲代码」 前言 ...

  6. Spring框架的灵魂IOC和AOP

    2019独角兽企业重金招聘Python工程师标准>>> 关键词 名称 模式 实现方式 IOC 依赖注入或控制反转 工厂模式 sessionfactory注入 AOP 面向切面编程 代 ...

  7. 通过自定义注解与aop统一存储操作记录

    模块开发完成后,接到通知需要添加操作记录功能,看着那一堆接口,如果一个方法一个方法的加,那真是太麻烦了.为了偷懒,就百度了一下,发现可以通过自定义注解和aop的形式来统一添加操作记录,只需要在每个方法 ...

  8. 一个简单的例子,学习自定义注解和AOP

    转载自   一个简单的例子,学习自定义注解和AOP 记得今年年初刚开始面试的时候,被问的最多的就是你知道Spring的两大核心嘛?那你说说什么是AOP,什么是IOC?我相信你可能也被问了很多次了. 1 ...

  9. 自定义注解实现权限校验含义_厉害了!老大利用AOP实现自定义注解,半小时完成我三天工作量...

    前面我们已经介绍了AOP适用的一些业务场景以及简单的代码实现,当时我们的切点是用execution表达式来配置的,这种方式有一些局限性在里面: 灵活性不高,一个表达式只能切到某种同类型的方法 个性化不 ...

最新文章

  1. 【廖雪峰python进阶笔记】类的继承
  2. 「 每日一练,快乐水题 」1984. 学生分数的最小差值
  3. C++后台开发需要什么?
  4. 技术干货 | 轻松两步完成向 mPaaS 小程序传递启动参数
  5. python 英语词频统计软件_为了边看美剧边学英语,我写了个字幕处理脚本
  6. ElasticSearch概述和定义
  7. oracle adf 自定义lov,Oracle ADF table列自定义过滤组件的方法
  8. Hadoop+Spark 集群部署
  9. Add-In运行机制解析(下)
  10. 只保留日期_全历手表与年历腕表都能看日期,为何唯独万年历腕表价格不菲?...
  11. android代码删除wifi,Android Wifi的forget()操作实例详解_Android_脚本之家
  12. vs+python 更新pip 成功解决You are using pip version 9.0.3, however version 10.0.1 is available.
  13. 干货 | 收藏贴!一文看懂静态资源服务沉浮及其在携程的演进
  14. HTML5已定稿:将彻底颠覆原生应用
  15. HTL/TTL转光纤模块
  16. Uber无人车愈挫愈勇 估值百亿 已运送数万乘客
  17. 顺势而为,人生会更轻松
  18. ubuntu和mac进行文件传输 运用scp命令
  19. 是国际体育活动助长全球不快乐
  20. 攻防世界mfw解题wp

热门文章

  1. mysql数据库密码破解方法
  2. mysql association_MyBatis association的两种形式——MyBatis学习笔记之四
  3. 如何免费制作小程序?
  4. 利用 Echarts 简单制作省份或区域地图步骤
  5. 关于无刷电机驱动器的布板建议(FOC)
  6. DHCP 服务原理与配置
  7. 对象和String之间转换(包含基本类型转换)
  8. 基于盲估计和ICA的单通道盲分离算法–Matlab仿真
  9. 4.Executor执行器
  10. 洛谷 洛谷 P2708 硬币翻转(高端算法)