编写第二个Spring程序——AOP实现
第二个Spring程序 AOP范例
1、新建maven工程
2、在pom.xml文件导入相关jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.6.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.6</version></dependency>
3、在 Packge【service】下创建 【ProductService】类:
package service;public class ProductService {public void doSomeService(){System.out.println("doSomeService");}
}
4、在 xml 文件中装配该 bean:
<bean name="productService" class="service.ProductService" />
5、在【TestSpring】中编写测试代码,运行:
package test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Source;
import service.ProductService;public class TestSpring {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});ProductService productService = (ProductService) context.getBean("productService");productService.doSomeService();}
}
运行结果
doSomeService
6、在 Packge【aspect】下准备日志切面 【LoggerAspect】类:
package aspect;import org.aspectj.lang.ProceedingJoinPoint;public class LoggerAspect {public Object log(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("start log:" + joinPoint.getSignature().getName());Object object = joinPoint.proceed();System.out.println("end log:" + joinPoint.getSignature().getName());return object;}
}
7、在 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"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean name="productService" class="service.ProductService" /><bean id="loggerAspect" class="aspect.LoggerAspect"/><!-- 配置AOP --><aop:config><!-- where:在哪些地方(包.类.方法)做增加 --><aop:pointcut id="loggerCutpoint"expression="execution(* service.ProductService.*(..)) "/><!-- what:做什么增强 --><aop:aspect id="logAspect" ref="loggerAspect"><!-- when:在什么时机(方法前/后/前后) --><aop:around pointcut-ref="loggerCutpoint" method="log"/></aop:aspect></aop:config>
</beans>
8、再次运行 TestSpring 中的测试代码,代码并没有改变,但是在业务方法运行之前和运行之后,都分别输出了日志信息:
start log:doSomeService
doSomeService
end log:doSomeService
编写第二个Spring程序——AOP实现相关推荐
- 编写第一个Spring程序——IOC实现
第一个Spring程序 IOC范例 1.新建maven工程 2.在pom.xml文件中导入相关jar包 <!-- https://mvnrepository.com/artifact/org.s ...
- 【Java框架型项目从入门到装逼】第二节 - Spring框架 AOP的丧心病狂解说,你喜欢露娜的月下无限连吗?
继续上一节的内容,多几个jar包: aop技术是面向切面编程思想,作为OOP的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想.AOP底层也是面向对象:只不过面向的不是普通的O ...
- 【Java框架型项目从入门到装逼】第二节 - Spring框架 AOP的丧心病狂解说,你喜欢露娜的月下无限连吗?...
继续上一节的内容,多几个jar包: aop技术是面向切面编程思想,作为OOP的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想.AOP底层也是面向对象:只不过面向的不是普通的O ...
- spring框架AOP的理解,程序高类聚的体现
本文主要介绍AOP思想,而不是Spring,Spring在本文只做为理解AOP的工具和例子,所以也不打算介绍Spring的Aspect.Join point.Advice.AOP proxy等概念,那 ...
- SSH框架之Spring4专题3:Spring与AOP
1 AOP的引入 1.1 Step1:项目aop_leadin1 先定义好接口与一个实现类,该实现类中除了要实现接口中的方法外,还要再写两个非业务方法,非业务方法也称之为交叉业务逻辑: doTrans ...
- Spring 使用AOP
AOP是Aspect Oriented Programming,即面向切面编程. 那什么是AOP? 我们先回顾一下OOP:Object Oriented Programming,OOP作为面向对象编程 ...
- Spring框架-AOP
1.什么是AOP? 面向切面编程,可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率 通过不修改源代码方式,在主干功能里面添加新功能 2 ...
- Spring IOC AOP详细笔记
spring 文章目录 spring 什么是spring spring框架作用: ioc aop spring基本配置 IOC掌握什么? IOC创建对象方式 Spring配置 别名配置 bean配置 ...
- 【SSM】Spring系列——AOP面向切面编程
文章目录 03 AOP面向切面编程 3.1 AOP概述 3.2 面向切面编程对有什么好处 3.3 模拟AOP框架实现 3.3.1 代码实现版本一 3.3.2 代码实现版本二 3.3.3 代码实现版本三 ...
最新文章
- 第一个PhoneGap程序以及错误解决
- CentOS-6.4安装配置Nginx
- POJ2155二维线段树
- IOS-awakeFromNib和viewDidLoad
- erwin 不能输入中文_国产开源建模软件PDMan与国外商业建模软件ERwin的主要功能比较...
- 右边菜单_AI基础教程65:使用文字菜单编辑文字(七)查找字体
- char数组转换成字符串_将字符串转换为char数组java –将字符串转换为char
- 计算机系相声剧本,大学相声剧本:两种学生
- python实现华容道游戏(v0.4)--支持游戏自动完成功能
- fan4801开关电源原理图_开关电源各模块原理实图讲解
- STM32 LoRaWAN探索板B-L072Z-LRWAN1入门指南
- jQuery cdn加速
- Python计算身体质量指数BMI
- You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).Please, commit your changes
- android百度地图定位跳转中心点,百度地图,拖动地图,定位marker固定在屏幕中心位置...
- java 获得唯一 数字_java生成唯一数字
- Debug的常用命令
- java毕业设计二手图书回收销售网站Mybatis+系统+数据库+调试部署
- pdf怎么转换成ppt?可以试试这三个方法
- CentOS7中安装Tomcat8
热门文章
- la环球乐园里的机器人_北京环球度假区核心工程完工,你知道这里面都有什么主题景区吗?...
- java $1参数_jmap命令详解----查看JVM内存使用详情
- php程序里的configini_程序员手册 修改php.ini的几种方法
- ajax怎么发送数据给php,ajax怎么发送数据给php
- wamp怎么安装mysql服务器_用wamp的mysq安装pythonmysql
- html 属于mvvm框架,前端MVVM框架avalon揭秘 - HTML编译器
- gogs可以自动化部署吗_三千、五千平方的仓库房可以用自动化立体仓库吗?
- java 纯面向对象_Java到底是不是一种纯面向对象语言?
- centos系统服务器关机,centos 7 重启服务器
- 19年的华为手机还能搭载鸿蒙吗,2019年华为手机出货或超2.15亿部 鸿蒙系统最快10月首秀...