spring切入点详解
Spring第三讲
内容:
1. spring切入点
2. advisor正则表达式
3. 自动代理
4. 使用aspect进行aop开发
5. 引入通知
6. 使用pojo+xml开发aop
7. Spring连接数据库
讲解:
1. spring切入点
定义切入点:如果不能表达在应用系统的什么地方应用
通知的话,通知将毫无用处,这就是切入点的用处。切入点决定了一个特定的类的特定方法是否满足一定的规则。若符合,通知就应用到该方法上, 如果1.返回true,2.被调用来决定MethodMatcher的类型。有两种类型:静态和动态。静态切入点的意思是通知总是被执行。如果一个切入点是静态的,该方法返回false.动态切入点根据运行时方法的参数值决定通知是否需要执行。如果切入点是动态的,该方法返回true。和1.方法类似,该方法也是在代理创建时运行一次。 如果切入点是静态的,3.永远不会执行,对于动态切入点,需要根据运行时的参数决定方法是否被通知,所以会增加系统的负担,尽量使用静态切入点。
使用Spring的静态切入点:
<bean id="nameMatchMethodPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >
<property name="mappedNames">
<list>
<value>study</value>
</list>
</property>
<property name="advice">
<ref bean="myAroundAdvice"/>
</property>
</bean>
2. advisor正则表达式
<!-- 采用正则表达式 -->
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>
.*tudy
</value>
</list>
</property>
<property name="advice">
<ref bean="myBeforeAdvice"/>
</property>
</bean><!-- 采用正则表达式 -->
<bean id="regexpMethodPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>
.*tudy
</value>
</list>
</property>
<property name="advice">
<ref bean="myBeforeAdvice"/>
</property>
</bean>
3. 自动代理
<bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Bean</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>
myAroundAdvice
</value>
</list>
</property>
</bean>
4. 使用aspect进行aop开发
1> 添加类库:aspectjrt.jar和aspectjweaver.jar
2>添加aop schema.
3>定义xml元素:<aop:aspectj-autoproxy>
4>编写java类,并用@Aspect注解成通知
AspectJ 支持 5 种类型的通知注解:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterReturning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行
5>配置成普通bean元素即可.
@Aspect
publicclass AspectUtil {
//简略写法
@Pointcut("execution(* *.work(..))")
publicvoid disp(){}
/**
* (* *.*(..)) // *包名+类名.*方法名
* (* *work(..)) //
* (public * *work(..)) public 修饰符 第一个*返回值类型 第二个*(包名+类名)work(..) //..参数任意
*/
@Before("disp()")
publicvoid getUp() {
System.out.println("...............该起床了........");
}
/**
* execution(* com.xyz.service.*.*(..))
* (* set*(..)) *返回值类型 (包名和类名) set方法名的开头*匹配所有
* (* cn.csdn.hr.service.EmployeeService.*(..))) 第一个*返回值 cn.csdn.hr.service.EmployeeService.*方法名(..参数任意)
* @throws Throwable
*/
@Around("disp()")
public Object punchCard(ProceedingJoinPoint jp) throws Throwable {
System.out.println(".............打卡................");
Object obj = jp.proceed();
System.out.println(".............打卡................");
return obj;
}
@AfterThrowing("disp()")
publicvoid unWell() {
System.out.println("...................需要请假...............");
}
@After("disp()")
publicvoid goHome() {
System.out.println(".............回家的路真漫长...............");
}
5. 引入通知
@Aspect
publicclass MyAspectAdive {
@DeclareParents(value="..")
private Auditable auditable;
}
6. 使用pojo+xml开发aop
优点:
基于注解的aspectj声明优先于xml配置.
基于xml的配置是spring专有的.aspectj得到越来越多的支持,
具备更好的重用性.
<!-- aop的配置 -->
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut expression="execution(* *.work(..))" id="myPointcut"/>
<aop:pointcut expression="execution(* *.work(..)) and args(name,pass)" id="myPointcut1"/>
<!-- 配置切面 -->
<aop:aspect ref="adviceUtil">
<aop:before method="goHome" pointcut-ref="myPointcut"/>
<aop:before method="checkLogin" pointcut-ref="myPointcut1"/>
<aop:around method="punchCard" pointcut-ref="myPointcut"/>
<aop:after method="getUp" pointcut-ref="myPointcut"/>
<aop:after-returning method="back" pointcut-ref="myPointcut"/>
<aop:after-throwing method="unWell" pointcut-ref="myPointcut" throwing="ex"/>
<aop:declare-parents types-matching="*..EmployeeServiceBean"
implement-interface="cn.csdn.advice.Auditable" default-impl="cn.csdn.advice.AuditableAdvice"/>
</aop:aspect>
</aop:config>
7. Spring连接mysql数据库
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${driverClassName}</value>
<context:property-placeholder location="jdbc.properties" />
2> jdbcTemplate模板类和nameedparamterJdbcTemplate模板类
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
<!-- nameedparamterJdbcTemplate模板类 -->
<constructor-arg ref="dataSource"/>
<bean id="adminDaoImpl" class="cn.csdn.dao.AdminsDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
<bean id="adminDaoNamedImpl" class="cn.csdn.dao.AdminDaoNamedImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"/>
spring切入点详解相关推荐
- 【Spring AOP】静态代理设计模式、Spring 动态代理开发详解、切入点详解(切入点表达式、切入点函数)
AOP 编程 静态代理设计模式 1. 为什么需要代理设计模式 2. 代理设计模式 名词解释 代理开发的核心要素 静态代理编码 静态代理存在的问题 Spring 动态代理开发 搭建开发环境 Spring ...
- Spring 体系结构详解
Spring 体系结构详解 核心容器(Core Container) Core和Beans模块提供了Spring最基础的功能,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对 ...
- spring注解详解与用法(总览)
这篇文章收集了我写的所有的spring注解的详细说明与用法,点击可以跳转到对应文章,此文章会不断更新 spring注解详解与用法(1)最基础也是最常见的如下所示,详情点击这里 @Controller/ ...
- Spring入门详解
typora-copy-images-to: upload Spring入门详解 Spring框架是Java开发中最常用的框架,功能非常强大 源码下载:Spring Framework jar包.文档 ...
- Spring AOP详解(转载)所需要的包
上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...
- Spring JDBC详解
<Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...
- [转载]Spring配置文件详解一:
2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Spring配置文件详解一:<context:annotation-config/>与<conte ...
- struts2+hibernate+spring配置详解
#struts2+hibernate+spring配置详解 struts2+hibernate+spring配置详解 哎 ,当初一个人做好难,现在终于弄好了,希望自学这个的能少走些弯路. 以下是自己配 ...
- Spring IoC详解
Spring IoC详解 原文地址:Spring IoC详解 写在最前 本文将主要写Spring最核心的部分,为什么写这篇的原因也是因为在刚开始学习Spring的时候,学得太粗糙了.感觉学了个皮毛,从 ...
最新文章
- 现身说法:37岁老码农找工作
- Oracle服务自动启动脚本
- 前后端分离之JWT用户认证
- 超链接js点击后页面向上滚动问题解决
- 列表推导(list comprehension)--Python
- html符号实体----介绍
- 务必了解的跨境电商ERP独立部署!
- java6不支持tlsv1.2_解决 JDK1.7 不支持 VCenter 6.7 的问题(涉及到Https TLS1.2协议)
- LocalDateTime日期格式之间的转换
- 复旦-华盛顿大学EMBA科创的奥E丨《神奇的材料》与被塑造的我们
- 10个值得推荐的免费设计模板网站
- iOS实现炫酷悬停交互视图
- kaggle初探--泰坦尼克号生存预测
- 微信小程序真机调试方法出现问题
- background-clip属性详解
- 《Hive权威指南》第六章:查询
- mysql gui vim_Vim 有什么奇技淫巧?
- 错过就要多花300元,MDCC 2016移动开发者大会5折抢票倒计时!
- java新应用_java8新特性的实际应用
- discuzx update sitekey.php,Discuz如何清除应用中心密码附加工具