Spring 中基于 AOP 的 @AspectJ注解实例
@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格。通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。
1.第一步:倒入jar包,跟上个例子包是一样的
aspectjrt.jar
aspectjweaver.jar
aspectj.jar
- aopalliance.jar
2.第二步:创建三个类
2.1这里是 Logging.java 文件的内容。这实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。
package com.spring.aop2; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect // 定义切面
public class Logging {
@Pointcut("execution(* com.spring.aop2.Student.*(..))") // 定义切点
private void selectAll() { }
/**
* 定义通知方法
*/ @Before("selectAll()")
public void beforeAdvice() {
System.out.println("----------beforeAdvice-----------"); } @AfterReturning(pointcut = "selectAll()", returning = "retVal")
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString());
} @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("There has been an exception: " + ex.toString());
} }
2.2下面是 Student.java 文件的内容:
package com.spring.aop2;
public class Student {
/**
* 定义学生类
*/
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void printAdvice() {
System.out.println("name:" + name + ",age:" + age);
}
}
2.3下面是 MainApp.java 文件的内容:
package com.spring.aop2; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
/**
* Spring aop基于注解 配置文件springAop.xml
* author:
* mail:2536201485@qq.com
* 时间:
*/
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/springAop.xml");
Student student=(Student)applicationContext.getBean("Student");
student.printAdvice();
} }
3.第三步:创建bean文件(上面的头文件在上个例子当中有,这里就省略了)
下面是配置文件 Beans.xml:
<!-- 开启注解 通过aop命名空间的<aop:aspectj-autoproxy
/>声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面-->
<aop:aspectj-autoproxy/>
<!-- 定义切面bean -->
<bean id="Logging" class="com.spring.aop2.Logging"></bean>
<!-- 配置bean -->
<bean id="Student" class="com.spring.aop2.Student">
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
</bean>
让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息(其中包含了异常通知):

Spring 中基于 AOP 的 @AspectJ注解实例的更多相关文章
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
- Spring中基于AOP的@AspectJ
以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/aspectj-based-aop-with- ...
- Spring 中基于 AOP 的 XML架构
Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...
- Spring中基于AOP的XML架构
以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...
- spring中基于aop使用ehcache
我就是对着这个博客看的 http://www.cnblogs.com/ctxsdhy/p/6421016.html
- spring中基于注解使用AOP
本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...
- Spring的AOP配置文件和注解实例解析
1.1 Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
随机推荐
- ajax按楼层加载数据
代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...
- Spring Data REST不完全指南(一)
简介 Spring Data REST是Spring Data项目的一部分,可轻松在Spring Data存储库上构建超媒体驱动的REST Web服务. Spring Data REST 构建在 Sp ...
- SpringBoot连接Redis服务出现DENIED Redis is running in protected mode because protected mode is enabled
修改redis.conf,yes 改为 no
- PHP出现SSL certificate:unable to get local issuer certificate的解决办法
当本地curl需要访问https时,如果没有配置证书,会出现SSL certificate: unable to get local issuer certificate错误信息. 解决办法: 1.下 ...
- get 获取方式练习题及dom基础
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 全网最全最细的fiddler使用教程以及工作原理
目录:导读 一.Fiddler抓包工具简介 二.Fiddler工作原理 三.Fiddler安装 四.Fiddler界面介绍 五.Fiddler菜单栏介绍 六.Fiddler工具栏介绍 七.Fiddl ...
- element动态添加表头的正确姿势
1. 第一步循环 el-table-column <el-table-column v-if="item.show" v-for="(item, index) in ...
- kafka高吞吐量之消息压缩
背景 保证kafka高吞吐量的另外一大利器就是消息压缩.就像上图中的压缩饼干. 压缩即空间换时间,通过空间的压缩带来速度的提升,即通过少量的cpu消耗来减少磁盘和网络传输的io. 消息压缩模型 消息格 ...
- python爬虫实战之爬取智联职位信息和博客文章信息
1.python爬取招聘信息 简单爬取智联招聘职位信息 # !/usr/bin/env python # -*-coding:utf-8-*- """ @Author ...
- bootstrap4中使用fontawesome5.6.3
先下载fontawesome5.6.3,选择free for web,下载完解压,丢在资源目录下 <form action=""> <div class=&quo ...