【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)一样,刚刚开始的时候,都是从简单的 ...
随机推荐
- python--12、pymysql模块
安装 pip3 install pymysql 使用方法(示例表为用户注册信息表): import pymysql user=input('username: ').strip() pwd=input ...
- (转) 淘淘商城系列——Redis五种数据类型介绍
http://blog.csdn.net/yerenyuan_pku/article/details/72855562 Redis支持五种数据类型:string(字符串),hash(哈希),list( ...
- unittest自定义运行全量case or 运行指定的单个或多个case
import unittest import os from case.zufang.test_api_area_rentProlist import Zf1 case_path = os.path. ...
- CAD得到所有实体1
主要用到函数说明: IMxDrawSelectionSet::AllSelect 得到当前空间的所有实体.详细说明如下: 参数 说明 [in,defaultvalue(NULL)] IMxDrawRe ...
- Mybatis逆向工程使用方法
使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件. 一.mapper生成配置文件 在generatorConfig.xm ...
- Java基础——二分法
BinarySearch 二分法查找,顾名思义就是要将数据每次都分成两份然后再去找到你想要的数据,我们可以这样去想,二分法查找很类似与我们平时玩的猜价格游戏,当你报出一个价格时裁判会告诉你价格相对于真 ...
- 我的第一次"闭包"应用
结论: 闭包可以当作强类型语言如C++.Java的全局变量使用,非常巧妙 需求: ssm项目,使用pagehelper分页,在写前一页.后一页.第一页.最后一页等页面跳转时,遇到了问题,如果查询全部的 ...
- Centos7二进制文件安装MySQL5.7.25
1.删除centos系统自带的mariadb数据库防止发生冲突 rpm -qa|grep mariadb rpm -e mariadb-libs --nodeps 2.安装libaio库 yum -y ...
- docker-ce安装官翻
参考http://www.cnblogs.com/maple42/p/5868846.htmlhttp://blog.csdn.net/lizehua123/article/details/50601 ...
- extract a page from a multiple pages pdf on Ubuntu OS
extract a page from a multiple pages pdf 1 extract a page from a multiple pages pdf use pdftk packag ...