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 ...
随机推荐
- Video tagging systems based on DNNs
Need: With the ever-growth large-scale video in the mobile phone, so what will everyone get from the ...
- 曹工说Redis源码(7)-- redis server 的周期执行任务,到底要做些啥
文章导航 Redis源码系列的初衷,是帮助我们更好地理解Redis,更懂Redis,而怎么才能懂,光看是不够的,建议跟着下面的这一篇,把环境搭建起来,后续可以自己阅读源码,或者跟着我这边一起阅读.由于 ...
- C++基础 学习笔记五:重载之运算符重载
C++基础 学习笔记五:重载之运算符重载 什么是运算符重载 用同一个运算符完成不同的功能即同一个运算符可以有不同的功能的方法叫做运算符重载.运算符重载是静态多态性的体现. 运算符重载的规则 重载公式 ...
- 在dwr的调用类里获取请求信息
在dwr的调用类里获取请求的相关信息HttpSession session = WebContextFactory.get().getSession();HttpServletResponse res ...
- shift count is too large
STM8S是8 bit单片机在STM8S中 unsigned long是32位, unsigned short和unsigned int都是16位,unsigned char是8位. 以以下代码编译时 ...
- linux下文件的打包和压缩
文章来源:linux下文件的打包和压缩 目录 一.文件压缩的原理 二.linux常见的压缩指令 三.常用实例 1.tar命令 2.zip命令 3.gz命令 4.bz2命令 5.xz命令(必须分两步) ...
- Linux安装jdk(详细教程)
一.JDK介绍 JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JA ...
- linux系统的简单配置
配置网卡:vim /etc/sysconfig/network-scripts/网卡名称 ifcfg-xxxx ##文件名称 DEVICE=xxx ##设备名称 BOOTPROTO=dhcp|st ...
- js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...
- layui模块化加载Echarts图表v4.2.1
layui.use(['jquery','echarts'], function () { var $ = layui.$; //记得这是dom对象不是JQ对象,需要转换 echarts = layu ...