【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)一样,刚刚开始的时候,都是从简单的 ...
随机推荐
- hanframe开微博了
之前一直在百度里转一些文章,平时都把积累的东西放在一些文档中,还是想着记下来会比较好一点,顺便,每天都来这里做一点总结吧
- 人人都能读懂的css3 3d小demo
css3 3d案例总结 最近入坑 Web 动画,所以把自己的学习过程记录一下分享给大家.就把最近做的比较好的给大家分享下 1.旋转拼图 首先看下效果 代码主要由HTML和CSS3组成,应该说还是比较简 ...
- Java屏幕截图及剪裁
Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能.这里只展示其屏幕截图. 截图的关键方法createScreenCapture(Rectangle rect) ,该 ...
- HDU_1421_搬寝室_dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421 搬寝室 Time Limit: 2000/1000 MS (Java/Others) Me ...
- R语言学习 - 线图绘制
线图是反映趋势变化的一种方式,其输入数据一般也是一个矩阵. 单线图 假设有这么一个矩阵,第一列为转录起始位点及其上下游5 kb的区域,第二列为H3K27ac修饰在这些区域的丰度,想绘制一张线图展示. ...
- ThinkPHP---插件highcharts
[一]概论 (1)介绍 基于jquery开发的国外图标插件,统计图,折线图,饼状图等常常用到. 国内也有一款类似插件echarts,由百度开发. (2)官网:www.highcharts.com ...
- php第二十三节课
XML XML:页面之间传递数据,跨平台传递 HTML:超文本标记语言,核心标签 XML特点:1.标签名可以自己定义2.有且只有一个根3.大小写敏感4.标签必须完整 <!DOCTYPE html ...
- Python面向对象,析构继承多态
析构: def __del__(self): print("del..run...") r1 = Role("xx") del r1 结果打印del..run. ...
- [bzoj1925][Sdoi2010][地精部落] (序列动态规划)
Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi, ...
- TestNG常用注解
原文链接:https://www.yiibai.com/testng/basic-annotations.html 以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该 ...