Spring(十二)--Spring AspectJ
Spring AspectJ
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ 是一个面向切面的框架!定义了AOP的语法!
Spring 将AspectJ 整合到了自己的框架中!
需要引入两个核心jar
01.aspectj.weaver
02.spring-aspects
(如果你使用的是idea 所需的所有pom节点,在这儿:https://www.cnblogs.com/fl72/p/9625697.html)
务必掌握的 AspectJ 的切入点表达式
execution([访问权限类型] 返回值类型 [完整限定类名]方法名 (参数) [抛出的异常类型])
execution( 返回值类型 方法名(参数))
*:0-N的字符
..:
01.如果是在方法参数中,表示参数可有多个或者可无
02.如果是在包名之后,表示当前包和子包
+:
01.如果定义在类后面,表示当前类以及子类
02.如果定义在接口后面,表示当前接口以及实现类
例子:

1. 使用注解实现
/**
* 当前类就是 整个程序中需要的各种系统级业务
* 就是一个切面类
*/
@Aspect
public class MyAspectJ { @Before("execution(* *..UserDao.sleep(..))")
public void before(){
System.out.println("前置增强........");
} @AfterReturning("execution(* *..UserDao.sleep(..))")
public void afterReturning(){
System.out.println("后置增强........");
}
//如果想获取方法的返回值
@AfterReturning(value = "execution(* *..UserDao.sleep(..))",returning = "result")
public void afterReturning(String result){
System.out.println("后置增强........"+result);
} /**
* 环绕增强可以改变返回值
*/
@Around("execution(* *..UserDao.eat(..))")
public Object around(ProceedingJoinPoint point){
System.out.println("环绕增强进来........");
Object result=null;
try {
result= point.proceed(); //执行目标方法
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕增强出去........");
return "orange";
}
}
2. 对应的xml文件和测试方法
<!--01.配置目标对象-->
<bean id="userDao" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置切面-->
<bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/> <!--03.注册aspectj的自动代理-->
<aop:aspectj-autoproxy/> //测试方法
@Test
public void defaultTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
UserDao dao= context.getBean("userDao", UserDao.class);
// System.out.println(dao.eat());
dao.sleep();
}
3. 使用纯切面的方式实现
public class MyAspectJ {
public void before(){
System.out.println("前置增强........");
}
public void afterReturning(){
System.out.println("后置增强........");
}
/**
* 环绕增强可以改变返回值
*/
public Object around(ProceedingJoinPoint point){
System.out.println("环绕增强进来........");
Object result=null;
try {
result= point.proceed(); //执行目标方法
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕增强出去........");
return "orange";
}
}
4.对应的xml文件和测试方法
<!--01.配置目标对象-->
<bean id="userDao" class="com.xdf.dao.UserDaoImpl"/> <!--02.配置切面-->
<bean id="myAspectJ" class="com.xdf.annotation.MyAspectJ"/> <!--03.注册aspectj需要的切入点-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="myPonit" expression="execution(* *..UserDao.sleep(..))"/>
<aop:pointcut id="myPonit2" expression="execution(* *..UserDao.eat(..))"/>
<!--配置切面-->
<aop:aspect ref="myAspectJ">
<aop:before method="before" pointcut-ref="myPonit"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPonit"/>
<aop:around method="around" pointcut-ref="myPonit2"/>
</aop:aspect>
</aop:config>
@Test
public void aspectJTest(){
ApplicationContext context=new ClassPathXmlApplicationContext("aspectJ.xml");
UserDao dao= context.getBean("userDao", UserDao.class);
//System.out.println(dao.eat());
dao.sleep();
}
未完待续!!!
Spring(十二)--Spring AspectJ的更多相关文章
- Spring(十二)使用Spring的xml文件配置方式实现AOP
配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...
- 十二 Spring的AOP开发入门,整合Junit单元测试(AspectJ的XML方式)
创建web项目,引入jar包 引入Spring配置文件
- 学习 Spring (十二) AOP 基本概念及特点
Spring入门篇 学习笔记 AOP: Aspect Oriented Programming, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 主要功能是:日志记录.性能统计.安全控 ...
- Java开发学习(二十二)----Spring事务属性、事务传播行为
一.事务配置 上面这些属性都可以在@Transactional注解的参数上进行设置. readOnly:true只读事务,false读写事务,增删改要设为false,查询设为true. timeout ...
- Spring(十二)之JDBC框架
JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...
- Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- Spring学习(十二)-----Spring Bean init-method 和 destroy-method实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- Spring Boot2 系列教程(三十二)Spring Boot 整合 Shiro
在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot ...
- Spring MVC(二)--Spring MVC登陆实例
本文通过一个简单的登陆实例实现Spring MVC的流程,同时整合 MyBatis使用,流程是这样的: 1.访问一个URL进入登陆界面 2.输入正确的用户名和密码,成功则进入index页面,否则留在登 ...
随机推荐
- Suitable Replacement
D. Suitable Replacement 这个题统计出 s 和 t 中的各字母个数以及"?"的个数,直接暴力即可,s中不足的字母可用 "?"来替代 这个题 ...
- Latex的beamer幻灯片图形不编号的问题
在beamer幻灯片中如果插入图形,一般不会显示图形编号,这是其默认模式,但我们可以通过设置给图形编号.解决办法是: 在导言区加上命令: \setbeamertemplate{caption}[num ...
- Laydate 使用注意事项
1.laydate 切记不能放在laytpl 模板语法中使用,否则可能会导致无法触发的情况 不在laytpl中使用 <div class="layui-form-item"& ...
- thinkphp is NULL表达式写法
thinkphp 中如果这样写 $where['status']=array('EQ','NULL'),打印出来sql是WHERE ( `status` = 'NULL' ):而我想要的是 `sta ...
- docker的数据管理
容器中管理数据主要有两种方式: 1.数据卷:容器内数据直接映射到本地宿主机. 2.数据卷容器:使用特定容器维护数据卷 数据卷: 数据卷是一个可供容器使用的特殊目录,他将主机操作系统目录直接映射进容器. ...
- 了解dubbo+zookeeper
一.Dubbo是什么? Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,分布式服务框架(SOA),致力于提供高性能和透明化的RPC远程 ...
- SRS之SrsRtmpServer::connect_app详解
1. connect('live') 2. SrsRtmpServer::connect_app 位于 srs_rtmp_stack.cpp.在 SRS 的 RTMP 连接处理线程 conn 中,当与 ...
- [Java]一段尚未雕琢的分词代码
package com.hy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStrea ...
- LNMPA是什么?
也许大家对LAMP.LNMP比较熟悉,LAMP代表Linux下Apache.MySQL.PHP这种网站服务器架构:LNMP代表的是Linux下Nginx.MySQL.PHP这种网站服务器架构.LNMP ...
- GitHub-Microsoft:sql-server-samples
ylbtech-GitHub-Microsoft:sql-server-samples 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://gi ...