SpringAOP 使用注解的简单使用
1. 导入jar包
/SpringAOPmy/lib/com.springsource.net.sf.cglib-2.2.0.jar
/SpringAOPmy/lib/com.springsource.org.aopalliance-1.0.0.jar
/SpringAOPmy/lib/com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
/SpringAOPmy/lib/commons-logging.jar
/SpringAOPmy/lib/junit-4.8.2.jar
/SpringAOPmy/lib/spring-aop-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-aspects-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-beans-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-context-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-core-4.1.0.RELEASE.jar
/SpringAOPmy/lib/spring-expression-4.1.0.RELEASE.jar
2.配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启自动扫描 --> <context:component-scan base-package="com.aspect.annonaction"></context:component-scan> <!--Spring 中使用aspectj 包中的@Aspect 注解标注的当前组件为切面 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
3. 注解切面方法
package com.aspect.annonaction; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class AOP { @Before("execution(* com.aspect.annonaction.Testss.say())") public void before() { System.out.println("前置通知"); } @AfterReturning(pointcut="within(com.aspect.annonaction.Testss)",returning="obj") public void afterreturning(JoinPoint jp,Object obj) { System.out.println(jp.getSignature()); System.out.println("返回值的类型:"+obj); } @AfterThrowing(pointcut="execution(* com.aspect.annonaction.Testss.say())",throwing="e") public void exception(Throwable e) { System.out.println("异常通知:"+e.getMessage()); } @Around(value="within(com.aspect.annonaction.Testss)") public void around(ProceedingJoinPoint pjp) { System.out.println("前置通知"); try { pjp.proceed(); System.out.println("后置通知"); } catch (Throwable e) { System.out.println("异常通知"); e.printStackTrace(); } System.out.println("最终通知"); } }
4. 测试
package com.aspect.annonaction; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; @Component public class Testss { public void say() { System.out.println("张韶涵"); } @Test public void test1 () { ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext2.xml"); Testss t=(Testss) ac.getBean("testss"); System.out.println(t); t.say(); } }
end
SpringAOP 使用注解的简单使用的更多相关文章
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- Java注解--实现简单读取excel
实现工具类 利用注解实现简单的excel数据读取,利用注解对类的属性和excel中的表头映射,使用Apache的poi就不用在业务代码中涉及row,rows这些属性了. 定义注解: @Retentio ...
- java基础强化——深入理解java注解(附简单ORM功能实现)
目录 1.什么是注解 2. 注解的结构以及如何在运行时读取注解 2.1 注解的组成 2.2 注解的类层级结构 2.3 如何在运行时获得注解信息 3.几种元注解介绍 3.1 @Retention 3.2 ...
- Springboot使用自定义注解实现简单参数加密解密(注解+HandlerMethodArgumentResolver)
前言 我黄汉三又回来了,快半年没更新博客了,这半年来的经历实属不易,疫情当头,本人实习的公司没有跟员工共患难, 直接辞掉了很多人.作为一个实习生,本人也被无情开除了.所以本人又得重新准备找工作了. 算 ...
- SpringAOP+注解实现简单的日志管理
今天在再次深入学习SpringAOP之后想着基于注解的AOP实现日志功能,在面试过程中我们也经常会被问到:假如项目已经上线,如何增加一套日志功能?我们会说使用AOP,AOP也符合开闭原则:对代码的修改 ...
- hibernate注解的简单应用
注解代替了我们用的*.hbm.xml文件.简少了我们的代码量:应用简单. @Override 用途:重写父类的同名方法 单元测试注解 @Test 用途:用于测试 @Before 用途:单测方法走之前执 ...
- 关于spring的IOC和DI的xml以及注解的简单介绍
xml 一 目的:通过ApplicationContext对象的getBean方法获取所需类的对象. 编写一个service类 public class service { private Strin ...
- Spring-AOP 基于注解的实现
一.AOP: 是对OOP编程方式的一种补充.翻译过来为“面向切面编程”. 可以理解为一个拦截器框架,但是这个拦截器会非常武断,如果它拦截一个类,那么它就会拦截这个类中的所有方法.如对一个目标列的代理, ...
随机推荐
- 083、Java数组之方法返回数组
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 嵊州普及Day5T4
题意:两个1,每次可将一个*k,一个*K2,n个问题,问能否达成x,y? 思路:只有将x,y相乘为3次方时,才可能.并且相乘的三次方一定要是x,y的因子. 下面证明:3次方易证,因为对每个k,都会乘三 ...
- 1.URLConnection
//爬虫://1.请求到某个网站去//2.返回一些HTML代码//3.从HTML代码提取你想要的信息 HTML解析//4.如果这些HTML中又有你感兴趣的内容//5.递归爬取//准备好网址 URL u ...
- BUU pwn cn
自己不细心,人家别的博客上写的明明没有那个冒号的,把linux命令好好学一学吧! nc后 ls 发现flag文件 cat就得到flag了
- hdu 1874 畅通工程续(SPFA模板)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- C语言常用函数
一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...
- Service IP 原理【转】
Service Cluster IP 是一个虚拟 IP,是由 Kubernetes 节点上的 iptables 规则管理的. 可以通过 iptables-save 命令打印出当前节点的 iptable ...
- java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver java.sql.SQLException
今天下午一直想用netbeans连接数据库,结果就是来来回回碰到这两个问题. 我还在想,连接数据库并不是一个什么困难的事情啊,我都按照教程上一步一步做的,代码什么的都感觉很好,怎么就找不到类呢,怎么就 ...
- 侯捷C++学习(一)
//c++学习//标准库非常重要//要规范自己的代码complex c1(2,1);complex c2;complex* pc = new complex(0,1);string s1(" ...
- [Java] Eclipse 设置相同变量背景色高亮显示
在Eclipse中,鼠标选中或者光标移动到java类的变量名时,相同变量会被标识显示(设置背景色高亮), 并且侧边滚动条会标出变量的位置, 查找变量十分方便. 1.相同变量标识高亮显示:Window ...