【Spring四】AOP之XML配置
1、在拦截器中,切入点的推断是很复杂的
2、尽管实现了切面与目标类的松耦合,可是在拦截器中还得实现结合过程
1、当启动spring容器的时候,
<bean id="classDao" class="cn.itheima03.spring.aop.xml.ClassesDaoImpl"></bean> <bean id="myTransaction" class="cn.itheima03.spring.aop.xml.MyTransaction"></bean> |
把这两个bean创建对象了
2、解析<aop:config>便签
(1)、解析切入点表达式<aop:pointcut>。切入点针对的是函数,从函数进行切入,把表达式解除出来以后和 spring中的bean进行匹配
(2)、假设匹配成功。则为该bean创建代理对象,在创建代理对象的过程中,把目标方法和通知结合在一起了
假设匹配不成功。则直接报错
(3)、当client调用context.getBean时,获取到的
(1)、假设该对象有代理对象,则返回代理对象
(2)、假设该对象没有代理对象。则返回对象本身
3、<aop:aspect>切面标签:
说明:
spring容器内部会自己主动推断:
假设目标类实现了接口。则採用jdkproxy
假设目标类没有实现接口,则採用cglibproxy
1、在目标方法之前运行
2、不管目标方法遇到异常都运行
后置通知:
1、在目标方法之后运行
2、假设目标方法遇到异常,则不运行
3、能够获取连接点的一些信息
终于通知:
1、相当于finally
2、不管目标方法是否遇到异常,都运行
异常通知
1、获取目标方法抛出的异常信息
2、throwing參数的配置才干获取信息
围绕通知
相当于jdkproxy的invoke方法
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!-- 1、目标类 2、切面 3、进行AOP的配置 -->
<bean id="classDao" class="cn.itheima03.spring.aop.xml.ClassesDaoImpl" ></bean>
<bean id="myTransaction" class="cn.itheima03.spring.aop.xml.MyTransaction" ></bean>
<aop:config >
<!-- 切入点表达式 expression切入点表达式
id 唯一标示 --> <aop:pointcut
expression="execution(*
cn.itheima03.spring.aop.xml.ClassesDaoImpl.*(..))" id= "perform" />
<!-- ref 引向切面
切面里包括各种各样的通知,这些通知都是我们自己想要额外实现的东西,比方开启事务等。。--> <aop:aspect ref= "myTransaction">
<!-- 方法运行之前运行
--> <aop:before method= "beginTransaction" pointcut-ref="perform" />
<!-- 后置通知
returning 返回值 要与方法中的參数的名字相相应 --> <aop:after-returning method= "commit" pointcut-ref="perform" returning="val" />
<!-- 终于通知
不管目标方法是否有异常。都运行 --> <aop:after method= "finnalyMethod" pointcut-ref="perform" />
<!-- 异常通知
throwing 获取目标方法抛出的异常信息 --> <aop:after-throwing method= "throwingMethod" pointcut-ref="perform" throwing="ex" />
<!-- 相当于
代理中invoke 方法,能够控制切入点的运行 --> <aop:around method= "aroundMethod" pointcut-ref="perform" />
</aop:aspect>
</aop:config >
</beans>
|
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) |
.png)
- execution(public * *(..)) 全部的公共方法
- execution(* set*(..)) 以set开头的随意方法
- execution(* com.xyz.service.AccountService.*(..))com.xyz.service.AccountService类里的全部的方法
- execution(* com.xyz.service.*.*(..)) com.xyz.service包下的全部类的全部的方法
- execution(* com.xyz.service..*.*(..)) com.xyz.service包及子包中全部的类的全部的方法
- execution(* cn.itheima03.spring..*.*(String,*,Integer))
- execution(* cn.itheima03.*.*.spring.*..*.*(..)) 參数..代表随意类型的随意參数,參数能够是0个
如cn.itheima03.a.b.spring.c.d.A.a()能匹配最后一个表达式。
public interface ClassesDao
{ public void saveClasses(Classes
classes); public void updateClasses(Classes
classes); public List<Classes>
getClasses(); }
===========================================
public class ClassesDaoImpl extends HibernateUtils{
public String
saveClasses(Classes classes) { int a =
1/0; sessionFactory.getCurrentSession().save(classes);
return "aaaa" ;
}
public List<Classes>
getClasses() { return sessionFactory .getCurrentSession().createQuery("from
Classes").list(); }
public void updateClasses(Classes
classes) { sessionFactory.getCurrentSession().update(classes);
}
}
===========================================
public class HibernateUtils
{ public static SessionFactory sessionFactory;
static{
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory =
configuration.buildSessionFactory(); }
}
===========================================
public class MyTransaction extends HibernateUtils{
private Transaction transaction;
/**
* 前置通知
* JoinPoint 可以调用该API得到连接点的一些信息
*/
public void beginTransaction(JoinPoint
joinPoint){ System. out.println(joinPoint.getSignature().getName());
this.transaction = sessionFactory.getCurrentSession().beginTransaction();
}
/**
* 后置通知
* 1、获取目标方法的返回值
*/
public void commit(Object
val){ System. out.println(val);
this.transaction .commit();
}
/**
* 终于通知
*/
public void finnalyMethod(){
System. out.println("finally
method" ); }
/**
* 异常通知
*/
public void throwingMethod(Throwable
ex){ /**
* 输出目标方法的异常信息
*/
System. out.println(ex.getMessage());
}
/**
* 围绕通知
* 1、假设不运行joinPoint.proceed();。目标方法是不运行的
* 2、在目标方法运行的上下文加入内容
*/
public void aroundMethod(ProceedingJoinPoint
joinPoint){ try {
System. out.println("aaaa" );
joinPoint. proceed();//运行目标方法
System. out.println("bbbb" );
} catch (Throwable
e) { e.printStackTrace();
}
}
}
===========================================
/**
* 注意的点
* 1、代理对象的方法体的内容就是拦截器 中invoke方法体的内容
* 2、在client,用代理对象调用方法的时候进去了invoke方法
*/
public class ClassesDaoTest
{ @Test
public void testSaveClasses(){
ApplicationContext context = new ClassPathXmlApplicationContext("cn/itheima03/spring/aop/xml/applicationContext.xml" );
ClassesDaoImpl classesDao = (ClassesDaoImpl)context.getBean("classDao" );
Classes classes = new Classes();
classes.setCname( "afds");
classesDao.saveClasses(classes);
}
}
|
/**
* 切面
* 日志管理
*/
public class Logger
{ public void interceptor()
{ System. out.println("logging" );
}
}
===========================================
/**
* 安全管理
*/
public class Security
{ public void interceptor()
{ System. out.println("security" );
}
}
===========================================
/**
* 权限管理
*/
public class Privilege{
private String access;
public String
getAccess() { return access ;
}
public void setAccess(String
access) { this.access =
access; }
public void interceptor(ProceedingJoinPoint
joinPoint) { if("admin" .equals(this.access)){
try {
joinPoint.proceed();
} catch (Throwable
e) { e.printStackTrace();
}
} else{
System. out.println("对不起,没有权限查看...." );
}
}
}
===========================================
/**
* 目标类
*/
public class SalaryManagerImpl implements SalaryManager{
@Override
public void showSalary()
{ System. out.println("正在查看工资" );
}
}
===========================================
public class SalaryTest
{ @Test
public void test(){
ApplicationContext context= new ClassPathXmlApplicationContext("cn/itheima03/spring/aop/multiaspect/applicationContext.xml" );
SalaryManager salarmManager=(SalaryManager) context.getBean("salaryManager" );
salarmManager.showSalary();
}
}
===========================================
|
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!--目标类, 切面类,aop
--> <bean id="salaryManager" class="cn.itheima03.spring.aop.multiaspect.SalaryManagerImpl" ></bean>
<bean id="logger" class="cn.itheima03.spring.aop.multiaspect.Logger" ></bean>
<bean id="security" class="cn.itheima03.spring.aop.multiaspect.Security" ></bean>
<bean id="privilege" class="cn.itheima03.spring.aop.multiaspect.Privilege" >
<property name= "access" value="admin" ></property>
</bean >
<aop:config >
<!--切入点 -->
<aop:pointcut expression="execution(*
cn.itheima03.spring.aop.multiaspect.SalaryManagerImpl.*(..))" id ="sm"/> <!-- 切面 -->
<aop:aspect ref= "logger">
<aop:before method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
<aop:aspect ref= "security">
<aop:before method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
<aop:aspect ref= "privilege">
<!--围绕切入点
--> <aop:around method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
</aop:config >
</beans>
|
【Spring四】AOP之XML配置的更多相关文章
- spring中aop以xml配置方式
1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...
- Spring中 aop的 xml配置(简单示例)
示例: aop,即面向切面编程,面向切面编程的目标就是分离关注点. 比如:小明(一位孩子)想吃苹果,首先得要有苹果,其次才能吃.那么妈妈负责去买水果,孩子负责吃,这样,既分离了关注点,也减低了代码的复 ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- spring之aop概念和配置
面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...
- Spring装配Bean---使用xml配置
声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- Spring之AOP在XML中的配置方法
AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutp ...
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- Spring AOP之xml 配置实现
首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...
随机推荐
- Unity笔记(2)自学第一天
学习记录: 界面使用:
- dubbo之服务容器
服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...
- [JSOI2012]玄武密码 题解(AC自动机)
显然是AC自动机对吧 插入单词之后把文章在自动机上跑一遍,到达过的节点打上花火标记 之后检查一下每个单词有几个标记即可 可以把题目中的4个字母映射成abcd方便遍历 一定要记得把文章也映射啊! #in ...
- 08Oracle Database 完整性约束
Oracle Database 完整性约束 非空约束 创建表时 Create table table_name( Column_name datatype NOT NULL,… ); 修改表时 Alt ...
- 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM
准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...
- 16监听器、Filter、Nginx、Spring、AOP
16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...
- Luogu P3802 小魔女帕琪
P3802 小魔女帕琪 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组合,从 ...
- trie字典树模板浅析
什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...
- 杂文Python
2.文件操作 文件操作的过程:打开文件获得句柄——>操作文件行(遍历等)——>关闭文件 打开文件获得句柄 比较low的方法: f = open("file_path", ...
- save density, pressure, velocity, temperature contour at one slice in xy plane-- paraview with batch Python scripts
#### import the simple module from the paraviewfrom paraview.simple import *#### disable automatic c ...