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 ...
随机推荐
- Linux性能优化 第七章 性能工具:网络
7.1 网络I/O介绍 Linux和其他主流操作系统中的网络流量被抽象为一系列的硬件和软件层次. 链路层,也就是最低的一层,包含网络硬件,如以太网设备.在传送网络流量时,这一层并不区分流量类型,而仅仅 ...
- Retrofit Token过期 重新请求Token再去请求接口
需求是这样的:请求接口A -- 服务器返回数据Token过期或失效 -- 重新请求Token并设置 -- 再去请求接口A 刚解决了这个问题,趁热打铁,写个博客记录一下:这个Token是添加到请求头里 ...
- android 开发 实现一个app的引导查看页面(使用ViewPager组件)
我们安装完app后第一次打开app,通常都会有一个翻页图片形式的app引导简介说明.下面我们来实现这个功能.ViewPager这个组件与ListView和RecyclerView在使用上有很高的相似处 ...
- ssh Socket error Event: 32 Error: 10053.
在家用的WiFi,把电脑从房间搬到餐厅来用发现用我的xshell不能用ssh连接了,报错Socket error Event: 32 Error: 10053.同时在自己物理机上ipconfig看到自 ...
- EXCEL中统计单元格内容出现次数
参考网站: https://jingyan.baidu.com/article/7c6fb428dfcc9580642c90ae.html 统计单元格内容出现次数是工作中经常会涉及到的问题. 那么,如 ...
- git 出现冲突时的解决办法
<一> 二者兼得最麻烦 1, 出现冲突一般出现在群体开发两个及以上开发者同时修改同一个文件时 2, 具体表现为 git pull , git push 和 git commit 命令执行失 ...
- 20165304《JAVA程序设计》第四周学习总结
教材内容总结 第五章 子类与继承 1.子类声明中通常用关键字extend来定义一个子类(class 子类名 extend 父类名{}) 2.子类和父类在同一包中的继承性,继承的成员变量或方法的访问权限 ...
- Photoshop 辅助线和标尺的使用技巧
1.拖动辅助线时按住Alt键可以在水平辅助线和垂直辅助线之间切换.按住Alt键点击一条已经存在的垂直辅助线可以把它转为水平辅助线,反之亦然. 注意:辅助线是通过从标尺中拖出而建立的,所以要确保标尺是打 ...
- Excel和Word 简易工具类,JEasyPoi 2.1.7 版本发布
JEasyPOI 简介 EasyPOI 功能如同名字easy,追求的就是简易,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出.Word模板导出,可以仅仅5行代码就可以完成Excel ...
- day37-常见内置模块六(其他模块)
其他内置模块 1.subprocess(系统交互)模块 2.math模块 3.zipfile模块 4.keyword模块 5....