aop注解 自定义切面的注解写法
spring.xml中
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 扫描注解bean -->
<context:component-scan base-package="cn.us.aspect"/>
<!-- 开启切面代理 使得spring认识 @Aspect -->
<aop:aspectj-autoproxy/>
java文件中 无参数的
package cn.us.aspect; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect")
@Aspect
public class LogAspect {
// @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) and args(user)") 以下是无参数的写法
@Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(..))")
public void pointcut()
{}
// @Before("pointcut(user)")
@Before("pointcut()")
// public void startRecordLog(User user)
public void startRecordLog()
{
System.out.println("Before log has started");
System.out.println();
} @AfterReturning(value="pointcut()",returning="val")
public void endRecordLog(Object val)
{
System.out.println("@AfterReturning log has end");
System.out.println(val);
}
}
有参数的 利用 @Before和@After写法
有参数 传递的写法 @Before和 @After 的形参都要写上 参数
package cn.us.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect")
@Aspect
public class LogAspect {
// @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)")
@Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) && args(user) ")
public void pointcut(User user)
{}
// @Before(value="pointcut(user)")
// public void startRecordLog(cn.us.domain.User user)
// @Before("pointcut()")
// public void startRecordLog()
@Before(value="pointcut(user)")
public void startRecordLog(User user)
{
System.out.println("Before log has started");
System.out.println(user);
} @AfterReturning(value="pointcut(user)",returning="val")
public void endRecordLog(User user,Object val)
{
User u=(User) val; //这里要强制类型转换,否则输出的位null ,没有强转调用object的,所以就为null了
System.out.println("@AfterReturning log has end");
System.out.println(u);
} @Around(value = "pointcut(user)")
public void aroundRecordLog(ProceedingJoinPoint pjp,User user) throws Throwable
{
System.out.println("around before");
//获取参数
Object [] objs=pjp.getArgs();
for(Object obj :objs)
{
System.out.println(obj);
}
//返回值
Object oo=pjp.proceed(); System.out.println("around after" +oo);
}
}
无参数的写法,利用环绕通知
可以再环绕通知里 利用getArgs()方法获取参数。并且输出
package cn.us.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect")
@Aspect
public class LogAspect {
// @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)")
@Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(..)) ")
public void pointcut()
{} @Around(value = "pointcut()")
public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("around before");
//获取参数
Object [] objs=pjp.getArgs();
for(Object obj :objs)
{
System.out.println(obj);
}
//返回值
Object oo=pjp.proceed(); System.out.println("around after" +oo);
}
}
aop注解 自定义切面的注解写法的更多相关文章
- 010-Spring aop 001-核心说明-拦截指定类与方法、基于自定义注解的切面
一.概述 面向切面编程(AOP)是针对面向对象编程(OOP)的补充,可以非侵入式的为多个不具有继承关系的对象引入相同的公共行为例如日志.安全.事务.性能监控等等.SpringAOP允许将公共行为从业务 ...
- 使用自定义注解和切面AOP实现Java程序增强
1.注解介绍 1.1注解的本质 Oracle官方对注解的定义为: Annotations, a form of metadata, provide data about a program that ...
- spring AOP 和自定义注解进行身份验证
一个SSH的项目(springmvc+hibernate),需要提供接口给app使用.首先考虑的就是权限问题,app要遵循极简模式,部分内容无需验证,用过滤器不能解决某些无需验证的方法 所以最终选择用 ...
- 用AOP拦截自定义注解并获取注解属性与上下文参数(基于Springboot框架)
目录 自定义注解 定义切面 获取上下文信息JoinPoint ProceedingJoinPoint 定义测试方法 测试结果 小结 AOP可以用于日志的设计,这样话就少不了要获取上下文的信息,博主在设 ...
- Spring自定义注解配置切面实现日志记录
一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...
- Spring 自定义注解,结合AOP,配置简单日志注解 (转)
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- Spring AOP:面向切面编程,AspectJ,是基于注解的方法
面向切面编程的术语: 切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象 通知(Advice): 切面必须要完成的工作 目标(Target): 被通知的对象 代理(Pr ...
- Spring AOP之使用注解创建切面
上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
随机推荐
- 在MyEclipse中修改类不重启tomcat
今天因为在调试一个程序,因为工程中用到spring,每次修改类代码时都要重启服务器,搞得很郁闷,于是上网找找有没有可以让java代码每次修改之后 直接加载到服务器的,找了一些还果真有,不过有些方法我试 ...
- 日期时间篇asctime ctime gettimeofday gmtime localtime mktime settimeofday time
asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime,localtime 表头文件 #include<time.h> 定义函数 char * asc ...
- 500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever
错误:500 OOPS: vsftpd: cannot locate user specified in 'chown_username':whoever解决方案:在vsftpd.conf中修改如下两 ...
- 【POJ】【3525】Most Distant Point from the Sea
二分+计算几何/半平面交 半平面交的学习戳这里:http://blog.csdn.net/accry/article/details/6070621 然而这题是要二分长度r……用每条直线的距离为r的平 ...
- Pytoch 抽取中间层特征方法
定义一个特征提取的类: 参考pytorch论坛:How to extract features of an image from a trained model from torchvision.mo ...
- 新鲜出炉!9个超高分辨率的iPhone 6原型素材打包下载
iPhone 6 出场,设计师又有得忙活了,但是新鲜的资源你们在哪里?!今天我们收集了一组精致的iPhone 6 模型素材,超高分辨率,多种视图,全都打包完毕,点一下就可以拿回家!赶紧来取吧!—— ...
- Ubuntu下安装Hadoop
终于把Hadoop的环境给配好了.在美国的第一个周末,非常的折腾,电脑坏了,一开机windows动画过后屏幕就没显示,无语死了,在想着人生地不熟的,哪里去找人修电脑,还好一个舍友说看到隔壁街有个PC ...
- Escape字符总结
有如下的 escape字符. 对于十进制来说,\后面只涵盖3个字符,比如\1234,是\123和字符4. 但是对于十六进制,后面会涵盖四个字符,比如\x1234,后面的四个字符都在\的涵盖范围内.
- 经典,HTML5游戏,超级玛丽
在线演示 在线演示 本地下载 这是一款使用HTML5开发的超级玛丽,有没有点儿时的记忆?长按向上键,可以跳的更高哦.如果你也喜欢可以当成休闲游戏,如果你是开发者,不防下载下来看看是如何生成的.
- Windows 之 防火墙
对于只使用浏览.电子邮件等系统自带的网络应用程序,Windows防火墙(firewall)根本不会产生影响.也就是说,用IE.OutlookExpress等系统自带的程序进行网络连接,防火 ...