十八、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最简单的用法,也是最常用的东西, ...
随机推荐
- 基于SVM的python简单实现验证码识别
验证码识别是一个适合入门机器学习的项目,之前用knn 做过一个很简单的,这次用svm来实现.svm直接用了开源的库libsvm.验证码选的比较简单,代码也写得略乱,大家看看就好. 1. 爬取验证码图片 ...
- android studio 的基本使用和建立一个小项目
https://github.com/allenxieyusheng/Android-Studio
- 详细解说Tomcat 设置虚拟路径的几种方法及为什么设置虚拟路径
说明:此次使用的是Tomcat 7.0 很多朋友都会很疑惑,既然我们都知道在Tomcat服务器上部署项目只要将项目打包,然后放到webapps目录下就可以了,为什么还需要配置虚拟路径?的确,把项目放到 ...
- CentOS6.5下搭建SVN服务器
1.检查是否已安装 rpm -qa | grep subversion 如果要卸载旧版本: yum remove subversion 2.安装 yum install subversion PS:y ...
- vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations
1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...
- 使用Hexo搭建一个简单的博客(二)
昨天想着用Hexo和github搭一个自己简单的博客,记录一下自己踩过的坑,具体的流程就不重复了,主要参考了一下几篇文章 GitHub+Hexo 搭建个人网站详细教程 使用Hexo+Github一步步 ...
- 《剑指offer》第五十九题(滑动窗口的最大值)
// 面试题59(一):滑动窗口的最大值 // 题目:给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值.例如, // 如果输入数组{2, 3, 4, 2, 6, 2, 5, 1}及滑动窗口的 ...
- win10新系统修改onedrive目录,提示找不到OneDrive目录
win10不知更新了什么,x1c非常卡一跳一跳的,很多年没见过了-_-!!( 原因排查:http://www.cnblogs.com/xuanmanstein/p/8878180.html). 于是重 ...
- JavaScript学习第一天(一)
JavaScript介绍 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...
- linux进程管理之信号控制
使用信号控制进程 ====================================================================================kill,ki ...