十八、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最简单的用法,也是最常用的东西, ...
随机推荐
- Java中泛型Class<T>、T与Class<?>、 Object类和Class类、 object.getClass()和Object.class
一.区别 单独的T 代表一个类型(表现形式是一个类名而已) ,而 Class<T>代表这个类型所对应的类(又可以称做类实例.类类型.字节码文件), Class<?>表示类型不确 ...
- Node.js代码模块化
js语言发展到现在逐渐的像后端语言来,学习了一些后端语言的特性,这里主要讲述的是js语言的模块化管理 首先新建一个js文件 'use strict'; var s = 'Hello'; functio ...
- vue--toutiao
git:https://github.com/vinieo/vue-toutiao 顶部导航栏 内容 底部导航按钮 组件
- ege demo
#include <ege.h> const float base_speed = 0.5f; const float randspeed = 1.5f; //自定义函数,用来返回一个0 ...
- Python 创建和使用类
python创建和使用类的方法如下 # class Dog(): # def __init__(self,name,age): # self.name=name # self.age=age # # ...
- Java Spring JDBC访问数据库
一.首先采用org.springframework.jdbc.datasource.DriverManagerDataSource类进行实现 1.applicationContext.xml配置如下: ...
- what are stop words
what are stop words 一.总结 一句话总结:就是在seo的关键词中不要有stop words,不然的话搜索引擎会直接忽略 stop words most common words ...
- Getting Started withProcessing 第八章总结
运动 在这一章中,作者讲述了如何对图元中的对象进行实现动画的效果. 实现运动的几种方式 在书中,作者通过讲解一些对应的知识,让图元能够产生移动的效果.这几种方式包括: 速度和方向 在全局变量中定义两个 ...
- Lasso linear model实例 | Proliferation index | 评估单细胞的增殖指数
背景:We developed a cell-cycle scoring approach that uses expression data to compute an index for ever ...
- 基于Struts2框架的文件下载 --- Struts2
一.Struts2指定类型文件的下载 1.最终功能实现的截图:(点击文件下载链接,下载文件 ) 2.核心代码 index.jsp: <%@ page language="java&qu ...