spring 中AOP的基本知识点
首先AOP就是一个动态代理,主要运用在事务控制,日志记录,安全控制等方面
1.连接点(Joinpoint):一个连接点 总是 代表一个方法的执行.
2.切入点(Pointcut):匹配连接点的 表达式
3.通知(Advice):连接点执行的动作 包括 执行前 执行后 环绕
通知的类型分为五种: 前置通知 返回后通知 抛出异常后通知 后通知 环绕通知
4.切面(Aspect): 连接点+切入点+通知=切面
5.目标对象(Target Object):就是委托类对象
6.织入(Weaving):动态代理的过程
使用xml配置AOP 实现添加日志操作
XML配置:
applicationContext-action.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
<bean id="userAction" class="com.cdsxt.action.UserAction" scope="prototype" >
<property name="userService" ref="userService" />
</bean>
</beans>
applicationContext-service.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
<bean id="userService" class="com.cdsxt.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</beans>
applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
<bean id="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" parent="baseDao" ></bean>
</beans>
applicationContext-resource.xml:一般把base资源性配置和公共性配置(比如连接数据库)配置在这里面,AOP的配置也在这里面
<?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.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
<bean id="baseDao" class="com.base.impl.BaseDaoImpl" lazy-init="true" ></bean>
<!-- 用aop完成以下逻辑:
执行service 层 所有类 的 add 方法 后 添加日志操作
步骤如下:
1:写一个类 并 在applicationContext-resource.xml内配置
2: 写aop:config
配置切入点
配置切面 ref="自定义的通知类"
配置通知类型
-->
<bean id="logAdvice" class="com.cdsxt.advice.LogAdvice" />
<aop:config >
<aop:pointcut expression="execution(public * com.cdsxt.service.impl.*.add*(..))" id="logCut"/>
<aop:aspect ref="logAdvice">
<!-- <aop:after method="addLog" pointcut-ref="logCut" />
<aop:before method="addBefore" pointcut-ref="logCut"/>
<aop:around method="addAround" pointcut-ref="logCut"/> -->
<aop:after-returning method="addReturn" pointcut-ref="logCut"/>
</aop:aspect>
</aop:config>
</beans>
base:
public interface BaseDao<T> {}
public class BaseDaoImpl<T> implements BaseDao<T> {
private Class clazz;
public BaseDaoImpl() {
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
clazz=(Class) type.getActualTypeArguments()[0];
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
}
action:
public class UserAction {
private UserService userService;
public void add(){
System.out.println("======UserAction=======");
userService.add();
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public static void main(String[] args) {
String[] rs = new String[]{"applicationContext-action.xml",
"applicationContext-dao.xml","applicationContext-resource.xml","applicationContext-service.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(rs);
UserAction u1= (UserAction) context.getBean("userAction");
u1.add();
}
}
service:
public interface UserService {
public void add();
}
public class UserServiceImpl implements UserService{
private UserDao userDao;
@Override
public void add() {
System.out.println("===========UserServiceImpl==========");
userDao.add();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
dao:
public interface UserDao extends BaseDao<User>{
public void add();
}
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{
@Override
public void add() {
System.out.println("======UserDaoImpl========");
}
}
po:
public class User {}
advice:
public class LogAdvice {
public void addReturn(){
System.out.println("正常返回后通知!!!");
}
public void addLog(){
System.out.println("完成了日志操作!!!");
}
public void addBefore(JoinPoint jp){
System.out.println("权限判断!!!!");
System.out.println(jp.getTarget().getClass());
}
public void addAround(ProceedingJoinPoint pjp) {
try {
System.out.println("环绕执行前");
pjp.proceed();
System.out.println("环绕执行后");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
spring 中AOP的基本知识点的更多相关文章
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- Spring 中aop切面注解实现
spring中aop的注解实现方式简单实例 上篇中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了. 注解方式实现aop我们 ...
- Spring中AOP相关源码解析
前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...
- AOP 与 Spring中AOP使用(上)
AOP简介 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的延续 ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- 浅析Spring中AOP的实现原理——动态代理
一.前言 最近在复习Spring的相关内容,刚刚大致研究了一下Spring中,AOP的实现原理.这篇博客就来简单地聊一聊Spring的AOP是如何实现的,并通过一个简单的测试用例来验证一下.废话不 ...
- Spring中AOP相关的API及源码解析
Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...
随机推荐
- python-day02-购物车
购物车 需求: 1.启动程序后,让用户输入工资,然后打印商品列表: 2.容许用户根据商品编号购买商品: 3.用户选择商品后,检测余额是否足够,够了就直接扣款,不够就提醒客户: 4.随时可以退出,退出时 ...
- 网页的缓存Cache与控制
什么是缓存 Cache? 缓存位于客户端与服务器之间, 或者服务器与服务器之间.它决定是否保存所获资源的副本,以及如何使用副本,何时更新副本,这里所说的资源包括页面的HTML, 图片,文件等等. 使用 ...
- 用socket发送信息在浏览器上显示出来
服务端代码: import socket def main(): sock=socket.socket() sock.bind(('localhost',8089)) sock.listen(5) w ...
- c# Color 颜色设置
#region 笔刷颜色 public SolidColorBrush col(byte a, byte r, byte g, byte b) { #region 颜色说明 //Color.FromA ...
- android 开发 实现一个进入相机拍照后裁剪图片或者进入相册选中裁剪图片的功能
实现思维路径: 以进入相机拍照的思维路线为例子: 1.进入app 2.判断之前是否保存头像,如果有就显示历史图像 (下面代码中在getOldAvatar();方法中执行这个逻辑) 3.点击更换图像的B ...
- 26.纯 CSS创作按钮被从纸上掀起的立体效果
原文地址:https://segmentfault.com/a/1190000014930183 感想:主要2D和3D转换.阴影效果. HTML代码: <nav> <ul> & ...
- <转载>apache 配置 http://www.blogjava.net/bukebushuo/articles/229103.html
基于 NCSA 服务器的配置文件 由 Rob McCool 编写,龙子翻译 Apache服务器主配置文件. 包括服务器指令的目录设置. 详见 <URL:http://www.apache.org ...
- linux path 与 classpath 区别
一.OS依据path中的路径信息来寻找可执行指令: 例如: cat /etc/profile 我们就可以在任意目录执行hadoop / hdfs / yarn / java 等相关命令了 export ...
- NIO,OIO,AIO区别
OIO中,每个线程只能处理一个channel(同步的,该线程和该channel绑定). 线程发起IO请求,不管内核是否准备好IO操作,从发起请求起,线程一直阻塞,直到操作完成,如图: NIO中,每个线 ...
- asp.net webform/mvc导出Excel通用代码
最近将自己在项目中经常用到的excel导出方法分析如下,如有不妥之处望他人指出,如果有更好的方法希望展示出来互相学习. //导出事件 protected void btnexcel_Click(obj ...