【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)一样,刚刚开始的时候,都是从简单的 ...
随机推荐
- 为什么字符串类型可以调用构造函数String的方法,却又不是它的实例
从所周知,在js中定义一个字符串我们有两种办法: var a = new String("a"); var a = "a"; 第一种方法使用构造函数创建,作为S ...
- ES6:Generator函数(1)
Generator函数是ES6提供的一种异步编程解决方案.它会返回一个遍历器对象 function* helloWorldGenerator(){ yield “hello”; yield “worl ...
- putty源码阅读----plink
一直对ssh协议的各种客户端实现比较入迷,遍寻了很多ssh协议实现也用了很多的库,发现依赖太多 putty是最纯洁依赖第三方几乎为0的客户端实现,先从plink处开始入手. 1.putty目录 才刚开 ...
- 16 this和super和构造代码块
this关键词---当前类的对象的引用 public class Public { String name; int age; public static void main(String[] arg ...
- Luogu P1297 [国家集训队]单选错位
P1297 [国家集训队]单选错位 题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...
- UVA - 11536 Smallest Sub-Array(尺取法)
题目: 思路: 读完题之后第一时间想到的是尺取法来做这个题,结果让自己写写崩了,还是练得少!! 到网上搜了一下学习了大佬的标记方法,用一个变量来判断是不是都已经出现,要比每次都判断一下快超多. 代码: ...
- Python学习-while循环练习
1.计算1-100的和 i = 1; total = 0; while i <= 100: total = total + i; i = i + 1; print(total); 2.打印出1- ...
- Oracle 控制文件(CONTROLFILE)
一.Oracle 控制文件 为二进制文件,初始化大小由CREATE DATABASE指定,可以使用RMAN备份 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等 ...
- Linux学习笔记(二) 文件管理
了解 Linux 系统基本的文件管理命令可以帮助我们更好的使用 Linux 系统,以下介绍几个常用的文件管理命令 1.pwd pwd 是 Print Working Directory 的简写,用于显 ...
- buf.writeFloatBE()函数详解
buf.writeFloatBE(value, offset[, noAssert]) buf.writeFloatLE(value, offset[, noAssert]) value {Numbe ...