该AOP开发入门案例采用XML文件方式配置开发(非注解方式)共包含一个xml文件和4个Java类,创建好web工程后引入相应jar包(文末会给出),建好包(若自定义包名注意更改类中的包名),将xml文件和Java类复制到包中即可通过测试类进行测试。

Spring的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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目标对象:被增强的对象 --><bean id="customerDao" class="cn.muc.spring.demo3.CustomerDaoImpl"></bean><!-- 将切面类交给Spring管理 --><bean id="myAspect" class="cn.muc.spring.demo3.MyAspectXML"></bean><!-- 通过AOP配置完成对目标类产生代理 --><aop:config><!-- 配置表达式:哪些类的哪些方法需要进行增强 --><aop:pointcut expression="execution(* cn.muc.spring.demo3.CustomerDaoImpl.save(..))" id="pointcut1"/><aop:pointcut expression="execution(* cn.muc.spring.demo3.CustomerDaoImpl.delete(..))" id="pointcut2"/><aop:pointcut expression="execution(* cn.muc.spring.demo3.CustomerDaoImpl.update(..))" id="pointcut3"/><aop:pointcut expression="execution(* cn.muc.spring.demo3.CustomerDaoImpl.find(..))" id="pointcut4"/><!-- 配置切面(通知) --><aop:aspect ref="myAspect"><!-- 前置通知 --><aop:before method="checkPri" pointcut-ref="pointcut1"/><!-- 后置通知,returning中自定义的字符串表示切入点(原方法)返回值的名字--><aop:after-returning method="writeLog" pointcut-ref="pointcut2" returning="result"/><!-- 环绕通知 --><aop:around method="watch" pointcut-ref="pointcut3"/><!-- 异常抛出通知 --><aop:after-throwing method="afterTrowing" pointcut-ref="pointcut4" throwing="ex"/><!-- 最终通知 --><aop:after method="after" pointcut-ref="pointcut4"/></aop:aspect></aop:config>
</beans>Dao接口代码:package cn.muc.spring.demo3;public interface CustomerDao {public void save();public String delete();public void update();public void find();
}

Dao实现类代码:package cn.muc.spring.demo3;public class CustomerDaoImpl implements CustomerDao {@Overridepublic void save() {System.out.println("保存方法被调用了。。。");}@Overridepublic String delete() {System.out.println("删除方法被调用了。。。");return "后置通知获得方法返回值";}@Overridepublic void update() {System.out.println("更新方法被调用了。。。");}@Overridepublic void find() {System.out.println("查找方法被调用了。。。");int a = 1 / 0;}}切面类代码:package cn.muc.spring.demo3;import org.aspectj.lang.ProceedingJoinPoint;/*** 切面类* @author xufaye**/
public class MyAspectXML {/*** 前置通知*/public void checkPri() {System.out.println("权限校验==================");}/*** 后置通知:可以获得方法返回值(通过传入对象类型的参数)*/public void writeLog(Object result) {System.out.println("日志记录===============" + result);}/*** 环绕通知:比如可以用于性能监控* @throws Throwable */public Object watch(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("环绕前通知=========");Object obj = joinPoint.proceed();System.out.println("环绕后通知=========");return obj;}/*** 异常抛出通知*/public void afterTrowing(Throwable ex) {System.out.println("异常抛出通知=========并获得异常信息:" + ex.getMessage());}/*** 最终通知:无论有没有异常都会执行,相当于finally代码块中的内容*/public void after() {System.out.println("最终通知===========");}
}测试类代码:package cn.muc.spring.demo3;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;/*** AOP入门* @author xufaye**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {@Resource(name="customerDao")private CustomerDao customerDao;@Testpublic void demo1() {customerDao.save();customerDao.delete();customerDao.update();customerDao.find();}
}

Java中AOP开发配置入门案例开发所需引入的jar包com.springsource.org.aopalliance-1.0.0.jar【AOP联盟】com.springsource.org.apache.commons.logging-1.1.1.jar【日志包】com.springsource.org.apache.log4j-1.2.15.jar【日志包】com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar【aspectj】spring-aop-4.2.4.RELEASE.jar【spring整合AOP包】spring-aspects-4.2.4.RELEASE.jar【spring整合aspectj包】spring-beans-4.2.4.RELEASE.jar【spring基础包】spring-context-4.2.4.RELEASE.jar【spring基础包】spring-core-4.2.4.RELEASE.jar【spring基础包】spring-expression-4.2.4.RELEASE.jar【spring基础包】spring-test-4.2.4.RELEASE.jar【spring整合Junit包】

---------------------
作者:曲水竹莲
来源:CSDN
原文:https://blog.csdn.net/bu529191519/article/details/88046616
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/xufaye/p/10459294.html

AOP配置开发入门案例相关推荐

  1. SpringBoot的Web开发入门案例7—WebMvcConfigurer配置类

    SpringBoot的Web开发入门案例7-WebMvcConfigurer配置类 WebMvcConfigurer接口的几个常用方法: addViewControllers:配置请求路径和页面的映射 ...

  2. SpringBoot的Web开发入门案例2—国际化

    SpringBoot的Web开发入门案例2-国际化 改造logintest项目:SpringBoot的Web开发入门案例1 地址:https://blog.csdn.net/BLU_111/artic ...

  3. SpringBoot的Web开发入门案例5—注册Servlets, Filter, Listener

    SpringBoot的Web开发入门案例5-注册Servlets, Filter, Listener 注册Servlet 创建MyServlet类 package com.blu.conf;impor ...

  4. SpringBoot的Web开发入门案例1

    SpringBoot的Web开发入门案例1-登录和页面数据遍历读取 新建maven项目:logintest pom.xml文件: <project xmlns="http://mave ...

  5. SpringBoot的Web开发入门案例9—数据访问

    SpringBoot的Web开发入门案例9-数据访问 创建一个springboot项目(打包方式为jar包): 勾选Spring Web选项,勾选JDBC API和MySQL Driver pom文件 ...

  6. SpringBoot的Web开发入门案例3—异常处理

    SpringBoot的Web开发入门案例3-异常处理 SpringBoot 默认404界面(由org.springframework.boot.autoconfigure.web.ErrorMvcAu ...

  7. SpringBoot的Web开发入门案例6—替换默认容器Tomcat

    SpringBoot的Web开发入门案例6-替换默认容器Tomcat为Jetty Spring Boot默认是使用Tomcat作为内嵌的Servlet容器的,如需修改为Jetty,只要修改pom文件即 ...

  8. Vue安装配置以及入门案例

    Vue Vue简介 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不 ...

  9. 【mybatis】学习笔记 1配置 搭建 入门案例

    将接口,普通的java对象映射成数据库中的记录 mybatis 简化 jdbc JDBC的数据连接 package com.mybatis;import java.sql.*;public class ...

最新文章

  1. GdiPlus[59]: 图像(十一) IGPImageAttributes 之颜色矩阵(TGPColorMatrix)变换
  2. Eclipse中的常用快捷键
  3. 信息安全官谁:逼近的挑战,你准备好了吗?
  4. python逆序数怎么求_怎么算逆序数?急~~~!!!
  5. 持续集成及部署利器:Go
  6. Learning to Track at 100 FPS with Deep Regression Networks 论文笔记
  7. 哈工大《工科数学分析》习题
  8. QQ游戏大厅的你画我猜游戏白屏问题解决
  9. word文件做一半未响应_word文档未响应文件还没保存该怎么处理?
  10. 初中数学503个必考知识点_高考数学必考知识点高中数学重点知识归纳
  11. win7锁屏背景壁纸修改
  12. 面向对象类和类之间的几种关系
  13. 【Maven】项目打包-war包-Jar包[IDEA将项目打成war包]
  14. 云计算笔记10day、11day
  15. 研究生计算机论文怎么写,研究生计算机论文摘要怎么写 研究生计算机论文摘要范文参考...
  16. Notion 上做英文阅读笔记
  17. 什么是RAID?RAID有什么用?RAID原理是什么
  18. b站江科大自化协51单片机入门教程笔记(2)
  19. Flink基础(十二):Parallelism 和 Slot 详解
  20. 京东又入AI科学家:裴健加盟任副总裁 负责大数据与产品研发 向刘强东汇报 | 消息

热门文章

  1. 缺陷的JIRA管理文档
  2. Rails测试《一》fixtures简介
  3. OpenStack潜力巨大:红帽打造生态系统
  4. 在项目实践中用更优雅的方式处理数组问题
  5. If-Modified-Since和If-None-Match
  6. oracle查询保留2位小数
  7. 深入理解Redis主键失效原理及实现机制
  8. url参数传递 java_URL中文参数传递问题
  9. 其实不的免费图标网站
  10. 如何将qlv格式倚天屠龙记转换为MP4格式