十八、Spring框架(AOP)
一、AOP(基于XML方式配置AOP)
AOP(Aspect Oriented Program):面向切面编程。思想是:把功能分为核心业务功能和周边功能。
所谓核心业务功能:比如登录,增删改数据都叫做核心业务
周边功能:比如性能统计,日志,事务管理等。
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发。然后再把他们两个"编织"在一起,就叫做AOP。
1、首先是jar包导入
为了支持AOP,需要用到一些额外的jar包。

2、开发思路图
2.1、功能分两大类:辅助功能和核心业务功能
2.2、辅助功能和核心业务功能彼此独立开发
2.3、比如说登录功能,就是没有性能统计和日志输出功能,也可以正常运行的
2.4、如果有需要的话,就可以把日志输出功能和登录功能编织在一起,这样登录的时候可以查看到日志输出。
2.5、辅助功能:也叫做切面功能,能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想叫做面向切面编程。

3、准备业务类ProductService
package com.demo.service;
public class ProductService{
public void doService(){
System.out.println("doService");
}
}
4、TestSpring测试
package com.demo.test; import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});。
ProductService service=(ProductService)context.getBean("service");//获取配置文件的ProductService属性name
service.doService();//doService
}
}
5、准备日志切面LoggerAspect
日志切面的功能:在调用核心功能之前和之后分别打印日志,所谓切面就是那些辅助功能。
package com.demo.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;
}
}
6、配置applicationContext.xml
配置ProductService业务对象。
<bean name="s" class="com.demo.service.ProductService"></bean>
配置日志切面
<bean id="loggerAspect" class="com.demo.aspect.LoggerAspect"></bean>
声明日志切面
<aop:pointcut id="loggerCutPoint" expression="execution(* com.demo.service.ProductService.*(..))"
/>
execution(* com.demo.service.ProductService.*(..))表示进行切面操作:
*:表示返回任意类型
com.demo.service.ProductService.*:表示包名以com.demo.service.ProductService开头的类的任意方法。
(..):表示参数是任意数量和类型。
指定核心功能:
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
具体的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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema.context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="category" class="com.demo.pojo.Category">
<property name="name" value="Apple"/>
</bean> <bean name="product" class="com.demo.pojo.Product">
<property name="name" value="Fruit"/>
<prpperty name="category" ref="category"/>
</bean>
<!--配置ProductService核心业务功能-->
<bean name="service" class="com.demo.service.ProductService"></bean>
<!--配置切面功能-->
<bean id="loggerAspect" class="com.demo.aspect.LoggerAspect"></bean> <aop:config>
<aop:pointcut id="loggerCutpoint" expression="(* com.demo.service.ProductService.*(..))"/>
<!--将切面功能编织在ProductService核心业务功能中-->
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>
7、测试切面功能编织到核心业务功能后
TestSpring:
package com.demo.test; import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}
二、AOP(基于注解方式AOP)
1、注解配置业务类
使用@Component("service")注解ProductService类,表明这个类是业务类
package com.demo.service;
import org.springframework.stereotype.Component;
@Component("service")
public class ProductService{
public void doService(){
System.out.println("doService");
}
}
2、注解配置切面
@Aspect注解:表示这是一个切面
@Component注解:表示这是一个bean,由Spring进行管理
@Around(value="execution(* com.demo.service.ProductService.*(..))")注解:表示对com.demo.service.ProductService这个类中的所有方法进行切面操作。
2.1、编写LoggerAspect(日志记录类)切面
package com.demo.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Compoent; @Aspect
@Component
publc class LoggerAspect{
@Around(value="execute(* com.demo.service.ProductService.*(..))")
publc 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;
}
}
3、修改applicationContext.xml配置文件
去掉原来的bean的配置,添加如下:
<!--作用:告诉Spring,实体类bean都放在com.demo.pojo这个包下,进行扫描-->
<context:component-scan base-package="com.demo.pojo"> <!--作用:告诉Spring,业务类都放在com.demo.pojo这个包下,进行扫描-->
<context:component-scan base-package="com.demo.service">
<!--作用:告诉Spring,切面类都放在com.demo.aspect这个包下,进行扫描-->
<context:component-scan base-package="com.demo.aspect">
<aop:aspectj-autoproxy/><!--进行业务类和切面类的编织-->
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.demo.aspect"/>
<context:component-scan base-package="com.demo.service"/>
<aop:aspectj-autoproxy/> </beans>
4、TestSpring测试
package com.demo.test; import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,通过在ProductService业务类的Component("service")注解获取到业务对象。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}
十八、Spring框架(AOP)的更多相关文章
- spring框架 AOP核心详解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
- 跟着刚哥学习Spring框架--AOP(五)
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- spring框架aop用注解形式注入Aspect切面无效的问题解决
由于到最后我的项目还是有个邪门的错没解决,所以先把文章大概内容告知: 1.spring框架aop注解扫描默认是关闭的,得手动开启. 2.关于Con't call commit when autocom ...
- Spring框架——AOP代理
我们知道AOP代理指的就是设计模式中的代理模式.一种是静态代理,高效,但是代码量偏大:另一种就是动态代理,动态代理又分为SDK下的动态代理,还有CGLIB的动态代理.Spring AOP说是实现了AO ...
- Spring框架-AOP详细学习[转载]
参考博客:https://blog.csdn.net/qq_22583741/article/details/79589910#4-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- Spring框架AOP学习总结(下)
目录 1. AOP 的概述 2. Spring 基于AspectJ 进行 AOP 的开发入门(XML 的方式): 3.Spring 基于AspectJ 进行 AOP 的开发入门(注解的方式): 4.S ...
- 08 Spring框架 AOP (一)
首先我们先来介绍一下AOP: AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,面向切面编程是从动态的 ...
- 09 Spring框架 AOP (二) 高级用法
上一篇文章我们主要讲了一点关于AOP编程,它的动态考虑程序的运行过程,和Spring中AOP的应用,前置通知,后置通知,环绕通知和异常通知,这些都是Spring中AOP最简单的用法,也是最常用的东西, ...
随机推荐
- Javascript 高级程序设计(第3版) - 第02章
2017-05-10 更新原文: http://www.cnblogs.com/daysme 在 html 中使用 js 把js代码写在 <script type="text/java ...
- HDU 4859 海岸线(最小割+最大独立点权变形)
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题意: 欢迎来到珠海!由于土地资源越来越紧张,使得许多海滨城市都只能依靠填海来扩展市区以求发展.作为Z市的 ...
- Log4j日志依赖
<!-- https://mvnrepository.com/artifact/log4j/log4j --><dependency> <groupId>log4j ...
- 基于 Python 和 Pandas 的数据分析(5) --- Concatenating and Appending
这一节我们将会介绍几种不同的合并数据的方法. 在我们这个不动产投资的例子中, 我们希望获取 51 个州的房产数据, 并把它们组合起来. 我们这样做有很多原因. 这样做既便于我们做分析, 同时也可以占用 ...
- 踩坑记录:spring boot的POST请求数据注入不了的问题
概述: 今天在使用spring boot框架的时候,踩了一个坑,是关于control层request body依赖注入的问题的,内容如下: 进过: 由于目前公司采用的系统架构,要求把springboo ...
- VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别
在使用VMware Workstation(以下简称:VMware)创建虚拟机的过程中,配置虚拟机的网络连接是非常重要的一环,当我们为虚拟机配置网络连接时,我们可以看到如下图所示的几种网络连接模式:桥 ...
- Linux简单入门
1.目录切换命令 cd usr 切换到该目录下usr目录 cd ../ 切换到上一层目录 cd / 切换到系统根目录 cd ~ 切换到用户主目录 cd - 切换到上一个所在目录 2.目录的操作命令 1 ...
- 基于反射实现实体DTO映射
对象类型转换还可以通过序列化和反序列化 先把一个对象序列化成字符串 然后反序列化成另外一个对象 通过表达式树 字段缓存 泛型缓存效率更高
- 学习笔记19—dpabi错误集
1.回归斜边量的时候千万不要用红色标记的地方,而要用紫色标记的地方
- git 放弃本地修改
本文以转移至本人的个人博客,请多多关注! 如果在修改时发现修改错误,而要放弃本地修改时, 一, 未使用 git add 缓存代码时. 可以使用 git checkout -- filepathnam ...